从数据库-口才-Laravel订购数据 [英] Order the data from Database- Eloquent - Laravel

查看:59
本文介绍了从数据库-口才-Laravel订购数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据列值在表中显示数据

I am trying to show a data in table based on column value,

我有一个表,列名为designation_id,我想根据ID对结果进行分组,并按特定顺序显示它,

I have table, col named designation_id, I would like to group the results based on the ID and show it in a particular order,

这是我目前为在刀片中达到目标而要做的工作,

This is what I currently do to achieve the result in my blade,

我的控制器

 $statemembers = StateChairman::find($id)->statemembers;

刀片

@foreach ($statemembers as $statemember)
    {{$statemember->first_name}}
<br>
<h4> President </h4>

@if ($statemember->designation_id == 'P')
{{$statemember->first_name}}
@else
    There are no members under the Chairman. Please add a new member under the chairman.    
@endif

<h4> Vice President </h4>
@if ($statemember->designation_id == 'VP')
{{$statemember->first_name}}
@else
    There are no members under the Chairman. Please add a new member under the chairman.    
@endif
@endforeach

等等

由于designation_id是动态的,因此我想为我动态选择它.

Since the designation_id is dynamic, I would like to pick it for me dynamically.

模型

class Statechairman extends Model
{
    public function statemembers(){
        return $this->hasMany('App\Statemembers','chairman_id','id');
    }
}


class Statemembers extends Model
{
    public function statechairman(){
        return $this->belongsTo('App\Statechairman','chairman_id','id');
    }

推荐答案

您可以通过这种方式订购关系数据.

You can order your relations data in this way.

$statemembers = StateChairman::find($id)->statemembers()->orderBy('your_column')->get();

这篇关于从数据库-口才-Laravel订购数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆