DEV Community

Lloric Mayuga Garcia
Lloric Mayuga Garcia

Posted on

1

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

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay