0

I'm making a query where the parent model has several relationships.

My query returns the correct data when i make only one relationship that o want to filter, the filter works normally.

some code before...

 ->with(['tickets' => function ($query) {
        if (($this->request->has('tickets_initial_date') && !is_null($this->request->get('tickets_initial_date'))) && ($this->request->has('tickets_final_date') && !is_null($this->request->get('tickets_final_date')))) {
            $query->whereBetween("tickets.created_at", ["{$this->request->get('tickets_initial_date')} 00:00:00", "{$this->request->get('tickets_final_date')} 23:59:59"]);
        } else if ($this->request->has('tickets_initial_date') && !is_null($this->request->get('tickets_initial_date'))) {
            $query->where("tickets.created_at", ">=", "{$this->request->get('tickets_initial_date')} 00:00:00");
        } else {
            $query->where("tickets.created_at", "<=", "{$this->request- 
       >get('tickets_final_date')} 23:59:59");
        }
}])->get();

If I add other relationships in with() it doesn't filter the right data and returns everything.

the code that returns everything without my filters.

->with(['tickets' => function ($query) {

if (($this->request->has('tickets_initial_date') && !is_null($this->request- 
>get('tickets_initial_date'))) && ($this->request->has('tickets_final_date') && 
!is_null($this->request->get('tickets_final_date')))) {
    $query->whereBetween("tickets.created_at", ["{$this->request- 
>get('tickets_initial_date')} 00:00:00", "{$this->request->get('tickets_final_date')} 
23:59:59"]);
} else if ($this->request->has('tickets_initial_date') && !is_null($this->request- 
>get('tickets_initial_date'))) {
    $query->where("tickets.created_at", ">=", "{$this->request->get('tickets_initial_date')} 00:00:00");
} else {
    $query->where("tickets.created_at", "<=", "{$this->request->get('tickets_final_date')} 23:59:59");
}

}])->with(['notes', 'groups.user'])->get();

the only problem is when i add more relationships as you can see in the last block of code above.

code that i add to get the rest of the relationship:

->with(['notes', 'groups.user'])->get();

I don't know what is wrong, it works, but not with the last with() relationship :

->with(['notes', 'groups.user'])->get();

1 Answers1

0

Did you try this?

->with(['tickets' => function ($query) {
    //your code
}, 'notes', 'groups.user'])->get();
omar jayed
  • 850
  • 5
  • 16
  • There was a typo in my answer. There was a dot(.) before 'notes'. It should be a comma(,). Did you try it with comma or dot? – omar jayed Apr 07 '20 at 18:30