Laravel-medialibrary: Limit number of images in collection

Created on 8 Aug 2019  路  3Comments  路  Source: spatie/laravel-medialibrary

Is there a simple way to limit the number of images a model can hold in a given collection?

For example, my Business model has one logo, which I have used ->single() for, and up to 3 banner images. I would like to limit it so they cannot have more than 3, and any additional uploads would replace older ones.

enhancement good first issue up for grabs

Most helpful comment

I'd accept a pull request that adds a onlyKeepLatest($int) function on MediaCollection. When adding a new item to the collection only the latest $int number of media models in it are kept, the rest is deleted. The PR should have updated test and updated docs. The singleFile method should, under the hood, call onlyKeepLatests(1).

All 3 comments

I'd accept a pull request that adds a onlyKeepLatest($int) function on MediaCollection. When adding a new item to the collection only the latest $int number of media models in it are kept, the rest is deleted. The PR should have updated test and updated docs. The singleFile method should, under the hood, call onlyKeepLatests(1).

Working on it! Got the code ready, heading over to writing the tests in a minute.

This has been added in the latest released. Thanks for the PR @joshuadegier

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xron89 picture xron89  路  3Comments

amrnn90 picture amrnn90  路  3Comments

mokhosh picture mokhosh  路  3Comments

ideadx picture ideadx  路  4Comments

mohammad6006 picture mohammad6006  路  4Comments