skip to Main Content

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 › AdminEnvironmentCrudController@index
POST      admin/environment .................................................................................................... environment.store › AdminEnvironmentCrudController@store
GET|HEAD  admin/environment/create ........................................................................................... environment.create › AdminEnvironmentCrudController@create
POST      admin/environment/search ........................................................................................... environment.search › AdminEnvironmentCrudController@search
PUT       admin/environment/{id} ............................................................................................. environment.update › AdminEnvironmentCrudController@update
DELETE    admin/environment/{id} ........................................................................................... environment.destroy › AdminEnvironmentCrudController@destroy
GET|HEAD  admin/environment/{id}/details ..................................................................... environment.showDetailsRow › AdminEnvironmentCrudController@showDetailsRow
GET|HEAD  admin/environment/{id}/edit ............................................................................................ environment.edit › AdminEnvironmentCrudController@edit
GET|HEAD  admin/environment/{id}/show ............................................................................................ environment.show › AdminEnvironmentCrudController@show

2

Answers


  1. Chosen as BEST ANSWER

    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 IlluminateValidationValidationException($validator);
                return back()->withErrors()->withInput();
            }
        });
    }
    

  2. 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),
            ],
        ];
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search