Support the ongoing development of Laravel.io →
posted 10 years ago
Database
Last updated 2 years ago.
0

why your using first()?..can u try this?

$customers = Customer::with('activity')->orderBy('updated_at', 'desc')->take(1000)->get();
Last updated 2 years ago.
0

With first(), thats mean you only take 1 data, maybe you can remove that and follow @johnvic suggestion

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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();

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

shiroamada shiroamada Joined 13 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.