Z
Z
zjohypxgb2019-03-10 12:33:51
Laravel
zjohypxgb, 2019-03-10 12:33:51

Does the policy always return false?

Policy in Laravel always returns false.
Method in controller:

public function destroy(Model $model, DestroyModel $request)
{
    //
}

Request class:
public function authorize()
{
    $model = Model::find($this->route('model'));

    return $model && $this->user()->can('delete', $model);
}

Politics:
public function delete(User $user, Model $model)
{
    return $user->id === $model->user_id;
}

And it always returns false. I have already double-checked everything that is possible, the user_id matches in the database.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
zjohypxgb, 2019-03-10
@zjohypxgb

Error in Laravel documentation. It says to use $this->route('blabla'). In theory, it should return the value of the parameter, but due to the auto-completion function, the model is immediately inserted there, and not the value of the parameter. So if you use autocomplete, do this:

$model = $this->route('model');

return $model && $this->user()->can('delete', $model);

A
Andrey, 2019-03-10
@VladimirAndreev

Debug all code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question