Try using dd on the line before return to check what comes in. My best bet is that it does not hit the policy
public function view(Client $client, Product $product)
{
dd($client, $product);
return $client->id === $product->client->id;
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
So, i have two models, Client and Product, a Client can have many Products, and a Product belongs to a Client, in the URL i have http://localhost:8000/clients/15/products/153, my issue is when i change the product id in the URL, Laravel returns to me the client with id = 15, but a Product from another client that is not the 15.
For example: http://localhost:8000/clients/15/products/1, it returns to me the product 1 and client 15, but the client 15 doesnt have any product with id of 1, so, in my controller i did:
if ($client->id != $product->client->id) {
\App::abort(403);
}
And works fine, but i would like to do it with Policies or something like that, a better way, how can i do it?
I created a ProductPolicy and in the view method i did:
public function view(Client $client, Product $product)
{
return $client->id === $product->client->id;
}
But when i use $this->authorize('view', [$client, $product]); on my ProductController, this give me error 403 forbidden all the time, even if the $product->client->id and $client->id matches.
Someone else knows the correct way to do it? What am i doing wrong?
How do I login? To work you need to be logged in. If that is not possible you need to set the User parameter as optional
https://laravel.com/docs/6.x/authorization#guest-users
public function view(?User $user, Product $product)
{
dd('asd');
}
And in controller change $client to $product
$this->authorize('view', $product);
This works.
Please or to participate in this conversation.