Hi i made relationship many-to-many and its working. I need make popularity so i want use groupBy and count with query. But i dont know how i can refer to relation table rent_car.
Controller:
public function popularityCar()
{
$total_raw = DB::raw('count(*) as total');
$cars = User::with('rentcar')
->where('rentcar')
->select('car_id', $total_raw)
->groupBy('car_id')
->get();
dump($cars);
echo $cars;
return view('user.popularityCar',['cars' => $cars]);
}
Model User;
public function rentcar()
{
return $this->belongsToMany(Cars::class,'rent_car','user_id','car_id')->withTimestamps()->withPivot('start', 'end');
}
Model Cars:
public function caruser()
{
return $this->belongsToMany(User::class,'rent_car','car_id','user_id');
}
So my question is how i can use groupBy and count with function "with". I trying found it and i made somethink like i showing in my controller.