Laravel 模型方法中记录的顺序列表

Laravel 模型方法中记录的顺序列表,laravel,laravel-5,eloquent,laravel-5.3,Laravel,Laravel 5,Eloquent,Laravel 5.3,我有一个叫做“调查”的模型,里面有一个叫做“步骤”的方法 基本上,我在调查表和其他所谓的step_调查之间有一个关系。在我的方法中,除了使用名为“position”的列通过desc获得所有步骤的顺序外,我还需要如何实现更改模型上的方法 表: step_surveys: - id; - survey_id; - step_name - position; 找到答案: public function steps(){ $order = $this->hasMany(Ste

我有一个叫做“调查”的模型,里面有一个叫做“步骤”的方法

基本上,我在调查表和其他所谓的step_调查之间有一个关系。在我的方法中,除了使用名为“position”的列通过desc获得所有步骤的顺序外,我还需要如何实现更改模型上的方法

表:

step_surveys:
- id;
- survey_id;
- step_name
- position;
找到答案:

public function steps(){

        $order =  $this->hasMany(StepSurvey::class);
        return $order->orderBy('position','DESC');
    }

您不必将模型存储在变量中。您可以像这样链接orderBy方法:return$this->hasMany(StepSurvey::class)->orderBy('position','DESC');
public function steps(){

        $order =  $this->hasMany(StepSurvey::class);
        return $order->orderBy('position','DESC');
    }