Order the data from Database- Eloquent - Laravel

2019-08-29 09:47发布

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

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,

My Controller,

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

Blade

@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

and so on,

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

Model,

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');
    }

1条回答
相关推荐>>
2楼-- · 2019-08-29 10:15

You can order your relations data in this way.

$statemembers = StateChairman::find($id)->statemembers()->orderBy('your_column')->get();
查看更多
登录 后发表回答