Home > front end >  multiple with with closure in laravel
multiple with with closure in laravel

Time:09-18

I'm trying to filter data first closure is for attachment and second closure for AdminAttachment

   $data = UserSalary::with(['userData','Attachments' => function($query) use($id) {
        $query->where('type', 3);
        $query->where('application_id',$id);
      }])->with(['AdminAttachments' => function($queryadminattachement) use($id) {
        $queryadminattachement->where('type', 3);
        $queryadminattachement->where('application_id',$id);
      }])->find($id);

but now i'm getting only Attachment data but AdminAttachment is blank but in my database data is there so it must return datas from both the attachment because search criteria is also same. Any solution please

Thanks

CodePudding user response:

Documentation has a section Eager Loading Multiple Relationships

Sometimes you may need to eager load several different relationships in a single operation. To do so, just pass additional arguments to the with method:

$books = App\Book::with(['author', 'publisher'])->get();

So dont call with method twice, just add new array value.

CodePudding user response:

Try this

 $data = UserSalary::with(['userData','Attachments','AdminAttachments'])
 
 ->where(function($handle) use($id){
    $handle->whereHas('Attachments',function($query) use($id) {
       $query->where('type', 3);
       $query->where('application_id',$id);
    })
    ->whereHas('AdminAttachments',function($queryadminattachement) use($id) {
       $queryadminattachement->where('type', 3);
       $queryadminattachement->where('application_id',$id);
    });
 })

->find($id);

Thanks

  • Related