I want to get relation:
Package::whereIn('id', $cart_items)->with('course')->select('id', 'name')->get();
It return successfully as object, but now I need to count this relation, I did:
function courses(){
return $this->hasMany('AppModelsCourse', 'package_id','id');
}
public function getCourseCount()
{
return $this->courses()->count();
}
And then:
Package::whereIn('id', $cart_items)->with('getCourseCount')->select('id', 'name')->get();
But give me this error:
Call to a member function addEagerConstraints() on int
Any idea?
—
I also used withCount
but it return empty array.
Package::whereIn('id', $cart_items)->withCount('courses')->select('id', 'name')->get();
2
Answers
withCount()
accepts a relation name as a parameter, so try thiswithout the
select()
From the docs:
So try the following code: