Laravel Voyager Scope query returning all rows

489 Views Asked by At

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??

1

There are 1 best solutions below

0
On

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;
    }

}