From 72ac8221e63c0a490eb3c376a4b8e7f880427ac9 Mon Sep 17 00:00:00 2001 From: Pedro X Date: Tue, 8 Aug 2023 15:54:05 +0100 Subject: [PATCH 1/2] add note about belongs to many relationships --- 6.x/crud-uploaders.md | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/6.x/crud-uploaders.md b/6.x/crud-uploaders.md index 2b60c7b2..a704e83c 100644 --- a/6.x/crud-uploaders.md +++ b/6.x/crud-uploaders.md @@ -36,7 +36,7 @@ CRUD::field('avatar') 'path' => 'uploads', // the path inside the disk where file will be stored ]); ``` -**Note**: If you've defined `disk` or `prefix` on the field, you no longer need to define `path` within `withFiles()` - it will pick those up. Make sure you are not defining both. +**Note**: If you've defined `disk` or `prefix` on the field, you no longer need to define `disk` or `path` within `withFiles()` - it will pick those up. Make sure you are not defining both. **Configuration options:** @@ -56,6 +56,43 @@ This allows you to overwrite or set the uploader class for this field. You can u - **`fileNamer`** - default: **null** It accepts a `FileNameGeneratorInterface` instance or a closure. As the name implies, this will be used to generate the file name. Read more about in the [Naming uploaded files](#upload-name-files) section. + +### Handling uploads in relationship fields + +Some relationships require additional configuration to properly work with the Uploaders. + +- **`BelongsToMany`** + +In this relationships, you should create a Pivot model where Uploaders register their events. + +Take for example an `Article` model has a `BelongsToMany` relationship defined with `Categories` model: + +```php +// Article model +public function categories() { + $this->belongsToMany(Category::class); +} +``` + +To use an Uploader in this relation, you should create the `ArticleCategory` pivot model, and tell Laravel to use it. + +```php +use Illuminate\Database\Eloquent\Relations\Pivot; + +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. +} +``` + + + ### Naming files when using Uploaders From bd1a822e89bd49ec1a0036297928ffaf8feca602 Mon Sep 17 00:00:00 2001 From: Pedro X Date: Wed, 9 Aug 2023 13:22:11 +0100 Subject: [PATCH 2/2] 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