Radical Design Course by Jack McDade

From the creator of Statamic

Learn how to make your websites standout and be remembered.

Taking your approach on designing things actually makes it fun, more natural, and overall easier.

— Dominik, Developer

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 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 →