From bd1a822e89bd49ec1a0036297928ffaf8feca602 Mon Sep 17 00:00:00 2001 From: Pedro X Date: Wed, 9 Aug 2023 13:22:11 +0100 Subject: [PATCH] add morphToMany --- 6.x/crud-uploaders.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/6.x/crud-uploaders.md b/6.x/crud-uploaders.md index a704e83c..074a2e46 100644 --- a/6.x/crud-uploaders.md +++ b/6.x/crud-uploaders.md @@ -87,11 +87,29 @@ class ArticleCategory extends Pivot // and in your article/category models, update the relationship to: public function categories() { - $this->belongsToMany(Category::class)->withPivot('picture')->using(ArticleCategory::class); //assuming picture is the pivot field where you store the uploaded file. + $this->belongsToMany(Category::class)->withPivot('picture')->using(ArticleCategory::class); //assuming picture is the pivot field where you store the uploaded file path. } ``` +- **`MorphToMany`** +Everything like the previous `belongsToMany`, but the pivot model needs to extend `MorphPivot`. + +```php +use Illuminate\Database\Eloquent\Relations\MorphPivot; + +class ArticleCategory extends MorphPivot +{ + +} + + +//in your model +public function categories() { + $this->morphToMany(Category::class)->withPivot('picture')->using(ArticleCategory::class); //assuming picture is the pivot field where you store the uploaded file path. +} + +``` ### Naming files when using Uploaders