Skip to content

Commit

Permalink
add morphToMany
Browse files Browse the repository at this point in the history
  • Loading branch information
pxpm committed Aug 9, 2023
1 parent 72ac822 commit bd1a822
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion 6.x/crud-uploaders.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

```

<a name="naming-files-when-using-uploaders"></a>
### Naming files when using Uploaders
Expand Down

0 comments on commit bd1a822

Please sign in to comment.