mercredi 5 février 2020

Laravel mix 5: Policy not working for certain functions

So i have my policy file set up to not allow anyone except the admin to add, update or delete customers. This id one like this:

//Create function
public function create(User $user)
    {
        return in_array($user->email, [
           'user@admin.com'
        ]);
    }

//Update function:
public function update(User $user, Customer $customer)
    {
        return in_array($user->email, [
            'user@admin.com'
        ]);
    }

//Delete function:
public function delete(User $user, Customer $customer)
    {
        return in_array($user->email, [
            'user@admin.com'
        ]);
    }

In my view I want to disable buttons according to my policy e.g admin is allowed to see buttons, normal users are not. This is dont in the following way:

//Index view: (create function)
@can('create', App\Customer::class)
                    <div class="card-footer">
                        <a href="" class="btn btn-success">Klant    toevoegen.</a>
                    </div>
                    @endcan()
                </div>

//Show view: (update and delete function)
@can('update', App\Customer::class)
                    <div class="card-footer">
                        <div style="float:left;">
                            <a href="" class="btn btn-info">Wijzigen</a>
                        </div>
                        @endcan()
                        @can('delete', App\Customer::class) 
                        <div style="float:right;">
                        <form action="" method="post">
                            @csrf
                            @method('DELETE')
                            <button class="btn btn-danger" type="submit">Verwijder</button>
                        </form>
                        </div>
                    </div>
                    @endcan()

This is working perfectly in my index.blade.php but for some reason I am not able to find out it's not working in my show.blade.php.

When accessing the show view i get this error:

Too few arguments to function App\Policies\CustomerPolicy::update(), 1 passed in /home/vagrant/code/Laravel/vendor/laravel/framework/src/Illuminate/Auth/Access/Gate.php on line 706 and exactly 2 expected (View: /home/vagrant/code/Laravel/resources/views/customer/show.blade.php)

I would gladly get some help and maybe an explanation as to why it is working in the index view but not in the show view.

Thanks in advance, Geert-Jan Knapen



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire