why your using first()?..can u try this?
$customers = Customer::with('activity')->orderBy('updated_at', 'desc')->take(1000)->get();
With first(), thats mean you only take 1 data, maybe you can remove that and follow @johnvic suggestion
why I want to take first, because I only want to take the latest activity date. and also one customer record only will be returned.
You can go in from the Activity side of the relationship. Get all the activities ordered how you want them then group them by customer_id to make it so only the latest activity is selected for each customer.
Maybe something like this:
$activities = Activity::with('customer')->orderBy('updated_at', 'DESC')->groupBy('customer_id')->take(1000)->get();
ok you have two options, maybe you could use an accessor or update your relationship activity something like this
return $this->hasMany('Activity')->orderBy('created_at','Desc')->first();
Accessors & Mutators http://laravel.com/docs/eloquent#accessors-and-mutators
Using accessor would be my preference.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community