dimanche 24 février 2019

Order the data from Database- Eloquent - Laravel

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)
    
<br>
<h4> President </h4>

@if ($statemember->designation_id == 'P')

@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')

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire