How to get latest eloquent relationship by column name in Laravel
Clash Royale CLAN TAG#URR8PPP
How to get latest eloquent relationship by column name in Laravel
I am building a small application on Laravel 5.6
where I am having two models Project
and Status
. In this I am having a relation as such:
Laravel 5.6
Project
Status
In Project Model I am having:
public function statusUpdate()
return $this->hasMany('AppStatus','project_id','id');
and to retrieve latest status I have:
public function latestStatus()
return $this->hasOne('AppStatus','project_id','id')->latest();
In status I have columns: date
, status
, sub_status
, comments
.
date
status
sub_status
comments
I want to retrieve Status
where I am having latest status
by date
mentioned in the column
Status
latest status
date
I tried doing this in my model:
public function latestStatus()
return $this->hasOne('AppStatus','project_id','id')->latest('date');
But this thing is not working out, help me out in this. Thanks
edit
I am using this relation in eager loading something like this:
Project::when( $request->name , function( $q) use( $request )
$q->where('name', 'like', '%' . $request->name .'%');
)->with('latestStatus')
->orderBy($request->sort_by_col, $request->order_by)
->paginate(30);
latestStatus()
have you tried using the following
statusUpdate()->latest()
?– Bertrand Kintanar
Aug 6 at 16:54
statusUpdate()->latest()
@JonasStaudenmeir yes. That is main problem.
– Nitish Kumar
Aug 6 at 16:54
Please post that code.
– Jonas Staudenmeir
Aug 6 at 16:55
@JonasStaudenmeir question updated.
– Nitish Kumar
Aug 6 at 16:58
3 Answers
3
You can use orderBy in the relationship.
public function latestStatus()
return $this->hasOne('AppStatus','project_id','id')->orderBy('date', 'desc');
Try it out.
This has
hasOne
relation will it order
by date
in desc
?– Nitish Kumar
Aug 6 at 18:17
hasOne
order
date
desc
@NitishKumar Yes, It will.
– Rutvij Kothari
Aug 6 at 18:58
->orderBy('date', 'desc')
is equivalent to ->latest('date')
.– Jonas Staudenmeir
Aug 6 at 20:23
->orderBy('date', 'desc')
->latest('date')
@JonasStaudenmeir I didn't know that. When I tried
latest('date')
it's not working. I don't know what's wrong with latest('date')
but orderBy('date', 'desc')
is working for me.– Rutvij Kothari
Aug 6 at 20:27
latest('date')
latest('date')
orderBy('date', 'desc')
You got your models wrong. This is what should be in the Project model
public function statuses() //plural because a project has many statuses
return $this->hasMany('AppStatus','id','project_id');
If you want the latest status, call this in your controller:
Project::where('name', 'like', "%$request->name%")->with('statuses', function($q)
return $q->orderBy('date', $request->order_by);
)->paginate(30);
If you want the latest project where the status has changed, first the Status model:
public function project()
return $this->hasOne('AppProject','project_id','id');
And in your controller:
$project = Status::latest()->first()->project;
You misunderstood the whole question. I want project with latest status as relation. I don't want the latest status with projects. Please go through my question once again.
– Nitish Kumar
Aug 6 at 18:16
Add first
to the end of the query.
first
public function latestStatus()
return $this->hasOne('AppStatus','project_id','id')->latest()->first();
This is how it works
statusUpdate
latest
first
limit 1
first() method doesn't add limit 1 to the query. First is retrieve all the record and then return the first one.
– Rutvij Kothari
Aug 6 at 17:02
@RutvijKothari, no, first() from the query builder does add limit 1.
get()->first()
would be what you're thinking of. However, this answer does not answer the question, it actually limits functionality because it is no longer returning a relationship.– Devon
Aug 6 at 17:14
get()->first()
@Devon Thanks for the clarification. I appreciate that. :) I thought first() works like this get()->first() but good to know that.
– Rutvij Kothari
Aug 6 at 17:36
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
How are you using
latestStatus()
? With eager loading?– Jonas Staudenmeir
Aug 6 at 16:54