How does Laravel check the latest relational model with specific conditions for a model?

Source: Internet
Author: User
My question asked by zookeeper on StackOverflow is to be answered. Stackoverflow. comques... a Package model with many Step models. There are two types of Step fields: tinyint status field and timestamp field. False configuration of a PackageA,... I just asked a question on StackOverflow, please answer.

Http://stackoverflow.com/ques...

A Package model has many Step models. There are two types of Step fields: tinyint status field and timestamp field.
False configuration of A Package A with the following steps

  • 18:; status: 1

  • 19, status is 2

False configuration of a Package B with the following steps

  • 19, status is 1

  • 19, status is 2

  • 19, status is 3

In this case, the latest step status of A is 2 and B is 3.

Then, how can I check the packages whose latest step status is 2?

I added zookeeper to the package model.

public function steps(){    return $this->hasMany('App\Step');}public function status(){    return $this->steps()->latest()->limit(1);}

Then, check the response.

Package::whereHas('status', function ($q) {    $q->where('status', 2);})->get();

However, pipeline cannot get the expected result.

The result is not the expected result. it indicates that, if the packages table has B in its dimension, I want to check the packages whose latest step status is 2, B is the latest 3, so the result should be blank. However, the result of this query is not empty, but contains B.

Then I continue to complete

public function status(){    return $this->hasOne('App\Step')->latest();}

The same is true.

Laravel, a beginner, is very confused about this problem. I also checked a lot of Google and did not have the desired answer. Hope you can help me!

Laravel 5.3

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.