Laravel breaks and finds OrFail does not return the correct string

I use the facade Gateto check if the user can delete the row or not.

 Gate::define('delete-cons', function ($user, $cons) {
        $c_id = Cons::findOrFail($cons)->pluck('c_id');
        return $user->c->id == $c_id;
    });

If I post $cons, it returns 4.

4   2   1   Mr  Joe Bloggs  male    1234567 0   1_1474822931.png    2017-01-02 17:33:49 2017-01-02 17:33:49 NULL

The first column above is the primary key, as we can see, it should return this row.

However, after the output $c_idafter the request, it shows:

#items: array:4 [▼
    0 => 1
    1 => 1
    2 => 1
    3 => 2
  ]

I would expect it to return the last result 2, but it will return all 4 rows in the table!

Even if I manually type in the row id to get:

$c_id = Cons::findOrFail(4)->pluck('c_id');

It still returns all rows.

I know I can use

$c_id = Cons::where('id', $cons)->pluck('c_id');

Which works correctly, but I follow the Laravel example Gatewhere FindOrFail throws an exception.

Many thanks.

+4
1

pluck() findOrFail(), , c_id . - :

$c_id = Cons::findOrFail($cons)->c_id;

- pluck(), :

$c_id = Cons::where('column', $cons)->pluck('c_id');
+2

Source: https://habr.com/ru/post/1665540/


All Articles