How to limit model instance creation

55 Views Asked by At

Using backpack/crud: 5.6.0 and laravel 9 here and I want to prevent a user from creating more then n items via the admin's ui "Add Environment" button with link https://example.com/admin/environment/create

When the user presses the button I want to show a simple message that the limit has been reached etc.

Where and how to implement this?

Thanks in advance for any suggestions.

Below are the routes for the Environment model:

GET|HEAD  admin/environment .................................................................................................... environment.index › Admin\EnvironmentCrudController@index
POST      admin/environment .................................................................................................... environment.store › Admin\EnvironmentCrudController@store
GET|HEAD  admin/environment/create ........................................................................................... environment.create › Admin\EnvironmentCrudController@create
POST      admin/environment/search ........................................................................................... environment.search › Admin\EnvironmentCrudController@search
PUT       admin/environment/{id} ............................................................................................. environment.update › Admin\EnvironmentCrudController@update
DELETE    admin/environment/{id} ........................................................................................... environment.destroy › Admin\EnvironmentCrudController@destroy
GET|HEAD  admin/environment/{id}/details ..................................................................... environment.showDetailsRow › Admin\EnvironmentCrudController@showDetailsRow
GET|HEAD  admin/environment/{id}/edit ............................................................................................ environment.edit › Admin\EnvironmentCrudController@edit
GET|HEAD  admin/environment/{id}/show ............................................................................................ environment.show › Admin\EnvironmentCrudController@show
2

There are 2 best solutions below

0
devunder On

Just stumbled upon https://laravel.com/docs/9.x/eloquent#events-using-closures and by using below function in the Environment model class it works.

protected static function booted()
{
    static::creating(function ($environment) {
        $environments = DB::table('environments')->get();
        if (count($environments) >= 3) {
            $validator = \Validator::make([], []);
            $validator->errors()->add('fieldName', 'No more then 3 environments please!');
            throw new \Illuminate\Validation\ValidationException($validator);
        }
    });
}
0
Jorge On

you can use validations to prevent user create more than X elements, something like this:

public function rules()
{
    return [
        // Other validation rules for your item fields...
        'name' => 'required|string|max:255',
        'description' => 'required|string',
        
        // Custom validation rule to check the number of user's creations
        'user_creations' => [
            'sometimes',
            'integer',
            Rule::unique('items')->where(function ($query) {
                return $query->where('user_id', auth()->id());
            }),
            Rule::max(2),
        ],
    ];
}