'Laravel Voyager Scope query returning all rows
I'm using laravel voyager with laravel 9 and I have an issue with the scope, the code is as follows:
public function scopeUser($query)
{ $products = DB::table('products')->select('id')->where('stakeholders_id', auth()->user()->id);
// dd($products);
$table = DB::table('product_variations')->whereIn('product_id',$products);
// dd($table);
return $table;
}
when I dd
the $table
variable I get the correct values needed when I add ->get()
at the end but when I remove both I get all the rows in the database when I need only the authenticated user's product variations, any ideas??
Solution 1:[1]
tray this
public function scopeUser($query)
{
$products = Product::where('stakeholders_id', auth()->id());
if (count($products) > 0) {
if (count($products) > 1) {
$pluckIds = $products->pluck('id')->toArray();
$table = DB::table('product_variations')->whereIn('product_id', $pluckIds)->get();
return $table;
}else{
$pluckIds = $products->first()->id;
$table = DB::table('product_variations')->where('product_id', $pluckIds)->get();
return $table;
}
}else{
return false;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Abdulmajeed |