Search code examples
laraveldatabaseeloquentmappingeloquent-relationship

Getting trouble in writing query laravel


Can anyone help me to get the data . 
$ticketList = Ticket::with('appliance', 'brand')->get();
$userAppliances = DB::table('user_appliances')
            ->where('user_id', 5)
            ->pluck('appliance_id')
            ->toArray();

$userBrands = DB::table('user_brands')
            ->where('user_id', 5)
            ->pluck('brand_id')
            ->toArray();

$ticketList = $ticketList->map(function ($ticket) use ($userAppliances) {
                return $ticket->where($ticket->appliance_id,'=', $userAppliances);
            });
            dd($ticketList);

It returnd the collection. I want all the tickets, where ticket->brand_id and tickt->appliance_id includes {all the id i am getting in array from userBrands and userAppliances}. I am getting trouble in writing query


Solution

  • Try it out this

    $userBrands = DB::table ('user_brands')
    ->where('user_id', 5)
    ->pluck('appliance_id')
    ->toArray();
    
    $ticketList = Ticket::with('appliance', 'brand')
       ->where(function($query) use ($userBrands){
         
         $query->whereIn('appliance_id', $userBrands);
    })->get();