Asset Repository

To work with the Asset Repository, use the following Facade:

use Statamic\Facades\Asset;

Methods

Methods Description
all() Get all Assets
find() Get Asset by filename
findByPath() Get Asset by path
findByUrl() Get Asset by url
query() Query Builder
whereContainer() Find Assets by AssetContainer
whereFolder() Find Assets in a filesystem folder
make() Makes a new Asset instance
Hot Tip!

You must specify the Asset Container when querying Assets.

Querying

Examples

Get all assets in a container

Asset::query()
->where('container', 'main')
->get();

Get all assets in a folder

Asset::query()
->where('container', 'main')
->where('folder', 'team_photos')
->get();

Get all assets without an alt tag

Asset::query()
->where('container', 'main')
->where('alt', null)
->get();

Get all Assets with "thumbnail" in the filename.

Asset::query()
->where('container', 'main')
->where('filename', 'like', '%thumbnail%')
->get();

Get the newest 10 assets from a container

Asset::query()
->where('container', 'main')
->orderBy('last_modified', 'desc')
->get();

Creating

Start by making an instance of an asset with the make method.
You need at least a path and the container before you can save an asset.

$asset = Asset::make()->container('assets')->path('images/hat.jpg');

Or, if you have an asset container instance, you can use the makeAsset method.

$asset = $container->makeAsset('images/hat.jpg');

Once you have an asset instance, you can add data to it.

$asset->data(['foo' => 'bar]);

Finally, save it. It'll return a boolean for whether it succeeded.

$asset->save(); // true or false
Hot Tip!

Saving an asset instance will only store the metadata. It doesn't actually create the asset itself.

You should manually place the asset at the corresponding location, or you can use the upload method which accepts an UploadedFile instance.

$file = $request->file('file');
$asset->upload($file);
HR: Section
Learn More!

Learn more about Asset Repository in these related docs:

HR: Section
Docs feedback

Submit improvements, related content, or suggestions through Github.

Betterify this page →