-2

Im trying to find the users that has commented the product this way:

public function getUserToRate(Product $product)
{
    $userIds = DB::table('product_ratings')->where('product_id', $product->id)->pluck('user_id');

    return $userIds;
}

But the thing is that $product->id is null. This is what comes out if I do:

DB::table('product_ratings')->where('product_id', $product->id)->toSql():
select * from `product_ratings` where `product_id` = ?

Why is my $product->id null? I have checked and the model exists, but I cant seem to use it inside the query builder.

Can anyone lend me a hand? Thanks

STA
  • 30,729
  • 8
  • 45
  • 59
Alberto
  • 61
  • 1
  • 9
  • 1
    how does that make you think it is `null`? – lagbox Jan 15 '21 at 03:05
  • Because in the query it is marked as ' ? ' so I'm assuming its a null value, isnt it? – Alberto Jan 15 '21 at 11:08
  • no, it means it is a parameter for a prepared statement, the SQL statement and its parameter are sent separately to the database .... look into "prepared statements" – lagbox Jan 15 '21 at 11:09

1 Answers1

0

I don't think you need a product model. So ignore Route Model binding like Product $product. Instead you can use your $oroductId. And query the UserIds like this:

public function getUserToRate( $productId)
{
    $userIds = ProductRating::where('product_id', $productId)->pluck('user_id');

    return $userIds;
}

    
Azahar Alam
  • 708
  • 5
  • 16
  • I've tried like this but it's not working anyways, the query still looks like this: select * from `product_ratings` where `product_id` = ? – Alberto Jan 15 '21 at 11:20
  • can I see your route that hit this function.and also the code you call that route – Azahar Alam Jan 15 '21 at 11:21