Skip to content

Commit

Permalink
Merge pull request #529 from awcodes/revert-523-3.x
Browse files Browse the repository at this point in the history
Revert "Support for collections & MorphOne polymorphic pivot table"
  • Loading branch information
awcodes authored Aug 19, 2024
2 parents 8359e49 + 6670371 commit 24378a6
Show file tree
Hide file tree
Showing 6 changed files with 1,414 additions and 2,923 deletions.
46 changes: 1 addition & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ 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 @@ -216,59 +215,16 @@ Model
```php
public function media(): MorphMany
{
// When you have multiple media per input.
// Example gallery images of a post

return $this->morphMany(Mediable::class, 'mediable')->orderBy('order');
return $this->morphMany(MediaItem::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 24378a6

Please sign in to comment.