DEV Community

Lloric Mayuga Garcia
Lloric Mayuga Garcia

Posted on

Tip to get collection data in run time using spatie/media-library in Laravel

Sample i want to check if its singleFile

In Model


    public function registerMediaCollections()
    {
        $this->addMediaCollection('homepage')
            ->registerMediaConversions(
                function (Media $media) {
                  // ...
                }
            );

        $this->addMediaCollection('page-large')
            ->singleFile() // < ----- this need to check
            ->registerMediaConversions(
                function (Media $media) {
                  // ...
                }
            );
    }
Enter fullscreen mode Exit fullscreen mode

In blade:

<?php /** @var \App\Models\CMS\Slider\Slider $model */ ?>

@php $model->registerMediaCollections() @endphp

@if(
    collect($model->mediaCollections)
    ->where('name', $model->collection_name)
    ->first()->singleFile
)
    <input-field name="image" type="image" checked/>
@else
    <input-field name="images" type="images" checked/>
@endif

Enter fullscreen mode Exit fullscreen mode

Top comments (0)