Laravel How do I find out the latest model with a specific condition for an item?

Source: Internet
Author: User
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();
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.