Reputation: 3287
in my sensory model i have this query helper method
public function asAroma()
{
return $this->belongsToMany('App\SensoryEvaluationForm')->wherePivot('sensory_type', '=', 2);
}
How can I add a query to this? I am using it like this
$cs = App\Sensory::first();
$cs->asAroma()->whereBetween('created_at', [$startingDate, $endingDate])->get();
It returns nothing. I think there is a different way on building queries with relationships?
What I'm trying to do is to add a wherebetween filter to the resulting relationship.
Upvotes: 0
Views: 878
Reputation: 3287
I already found an answer, it seems I need to specify the table name of the created_at attribute I want to refer to in this case sensory_evaluation_forms.created_at
Upvotes: -1
Reputation: 189
Use whereHas() and pass a callback to it.
CsModel::with('asAroma')->whereHas('asAroma', function($query) use ($start, $end) {
$query->whereBetween('created_at', [$start, $end]);
})->get();
Upvotes: 2