Mar 23, 2022
0
Level 3
WhereHas on Nested If gives strange Results
I'm having models Team , User , Packages , deliver
Team.php
class Team extends Model
{
public function membersWithPackages()
{
return $this->belongsToMany(User::class, 'teams_members', 'team_id', 'user_id',)
with('getPackagesWithDeliver')->where('status', '1')->orderBy('name');
}
}
User.php
class User extends Model
{
public function getPackagesWithDeliver()
{
return $this->hasMany(Package::class, 'user_id', 'id')->where('status', '7')
->with('lastDelivery');
}
}
Package.php
class Package extends Model
{
public function lastDelivery()
{
return $this->hasOne(Delivery::class)->latest();
}
}
I've implemented a scope function with trait that will filter the lastdelivery based on date
trait HasFilter
{
public function scopeDateFilter($query, $from, $to, $feild)
{
if ($from && $to) {
$from = $from->todateString();
$to = $to->todateString();
$query->whereBetween($feild, [$from, $to]);
} elseif ($from) {
$query->where($feild, '>=', $from);
} elseif ($to) {
$query->where($feild, '<=', $to);
}
}
}
In controller i'm getting teams with their users and Packages, In Front end user can select from and to dates So this will apply on lastdelivery of the package only that loads need to show in the result currently i'm getting all loads .
Controller.php
$teams = Team::withCount('members')
->whereHas('membersWithPackages.getPackagesWithDeliver.lastDelivery',
function ($q) use ($start_date, $end_date) {
$q->dateFilter($start_date, $end_date, 'date');
})->with('membersWithPackages')->get();
Please or to participate in this conversation.