I just asked the question in the StackOverflow for answers.
Http://stackoverflow.com/ques ...
A package model with many Step models. There are two of these in the Step field, the tinyint-type status field and the Timestamp field.
To assume a package A, there are the following steps
No. 18, 10, status 1.
No. 19, 9, status 2.
Suppose a package B has the following steps
No. 19, 8, status 1.
No. 19, 9, status 2.
No. 19, 10, status 3.
So the latest step status for A is 2,b is 3.
Then, how to find out the latest step status for the 2 packages?
I tried to add in the package model
public function steps(){ return $this->hasMany('App\Step');}public function status(){ return $this->steps()->latest()->limit(1);}
And then look at it.
Package::whereHas('status', function ($q) { $q->where('status', 2);})->get();
But it does not get the desired results.
What is not expected is that if, in the Packages table, Gion has a b this one, I want to find out the latest step status for the 2 packages,b the latest is 3 so the result is empty, but this way the results are not empty, but include B.
And then I tried to swap it out.
public function status(){ return $this->hasOne('App\Step')->latest();}
is still the same.
Just learning Laravel, it is very confusing to meet this problem, also looked up a lot of Google, there is no need to answer. I hope you help me, thank you so much!
5.3 of Laravel used
Reply content:
I just asked the question in the StackOverflow for answers.
Http://stackoverflow.com/ques ...
A package model with many Step models. There are two of these in the Step field, the tinyint-type status field and the Timestamp field.
To assume a package A, there are the following steps
No. 18, 10, status 1.
No. 19, 9, status 2.
Suppose a package B has the following steps
No. 19, 8, status 1.
No. 19, 9, status 2.
No. 19, 10, status 3.
So the latest step status for A is 2,b is 3.
Then, how to find out the latest step status for the 2 packages?
I tried to add in the package model
public function steps(){ return $this->hasMany('App\Step');}public function status(){ return $this->steps()->latest()->limit(1);}
And then look at it.
Package::whereHas('status', function ($q) { $q->where('status', 2);})->get();
But it does not get the desired results.
What is not expected is that if, in the Packages table, Gion has a b this one, I want to find out the latest step status for the 2 packages,b the latest is 3 so the result is empty, but this way the results are not empty, but include B.
And then I tried to swap it out.
public function status(){ return $this->hasOne('App\Step')->latest();}
is still the same.
Just learning Laravel, it is very confusing to meet this problem, also looked up a lot of Google, there is no need to answer. I hope you help me, thank you so much!
5.3 of Laravel used
Package::whereHas('steps', function ($q) { $q->where('status', 2);})->get();