Radical Design Course by Jack McDade

From the creator of Statamic

Learn how to make your websites standout and be remembered.

Bought Jack McDade's course on design. Going through it now...and it is SO well done!

— Justin Jackson, Transistor.fm

Asset Repository

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

use Statamic\Facades\Asset;

Methods

Methods Description
all() Get all Assets
find($filename) Get Asset by filename
findByPath($path) Get Asset by path
findByUrl($url) Get Asset by url
findOrFail($filename) Get Asset by filename. Throws an AssetNotFoundException when the asset cannot be found.
query() Query Builder
whereContainer($container) Find Assets by AssetContainer
whereFolder($folder) 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 path.

Asset::query()
->where('container', 'main')
->where('path', '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!

There is more to learn more in these related articles:

HR: Section
Docs feedback

Submit improvements, related content, or suggestions through Github.

Betterify this page →