Skip to content

Commit

Permalink
Merge pull request #523 from GeorgeFourkas/3.x
Browse files Browse the repository at this point in the history
Support for collections & MorphOne polymorphic pivot table
  • Loading branch information
awcodes authored Aug 19, 2024
2 parents 481054b + 8bd6764 commit 8359e49
Show file tree
Hide file tree
Showing 6 changed files with 2,911 additions and 1,402 deletions.
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ Schema::create('media_items', function (Blueprint $table) {
$table->foreignId('media_id')->constrained()->onDelete('cascade');
$table->integer('order');
$table->string('type');
$table->string('collection') // optionally you can add a collection
$table->timestamps();
});
```
Expand All @@ -215,16 +216,59 @@ Model
```php
public function media(): MorphMany
{
return $this->morphMany(MediaItem::class, 'mediable')->orderBy('order');
// When you have multiple media per input.
// Example gallery images of a post

return $this->morphMany(Mediable::class, 'mediable')->orderBy('order');
}
```
```php
// When you have you need 1 media item.
// To follow up the above example, this could be a post's featured image.

public function singleMedia()
{
return $this->morphOne(Mediable::class, 'mediable', 'mediable_type', 'mediable_id');
}
```

Pivot Model Mediable

```php
public function media()
{
return $this->belongsTo(Media::class);
}
```

#### Collections
The package now supports the use of collections. The use of collections is supported only when using Morphable relationships.
To start using collections add the following column in the Mediable model migration
```php
$table->string('collection') // optionally you can add a collection
```

The use of collection in your would result in a new relationship method.
Let's define a new collection with the name of Gallery Images and one of Featured Image
```php
public function featuredImage()
{
return $this->singleMedia()->where('collection', 'featured_image');
}


public function galleryImages()
{
return $this->media()->where('collection', 'gallery_images');
}
```
Form component

```php
CuratorPicker::make('document_ids')
->multiple()
->relationship('media', 'id')
->collection('gallery_images') // Optional: Rename the collection name if needed
->orderColumn('order') // Optional: Rename the order column if needed
->typeColumn('type') // Optional: Rename the type column if needed
->typeValue('document'); // Optional: Specify the type value if using types
Expand Down
Loading

0 comments on commit 8359e49

Please sign in to comment.