dimanche 24 février 2019

Output Eloquent data based on Designation Table

I am trying to output order a result as well as name the record to corresponding id,

What I am trying to achieve,

Chairman |
         |
         President - Heading
          - member name
         Vice President - Heading
          - member name

I have a chairman with subcordinates who are divided based on designation ID,

To acheive this, I have created 3table, statechairmen

  id | first_name | from | to  

    1 | Chairman | 2019 | 2020 

statemembers

  id | first_name | chairman_id | designation_id 

  1 | Member 1 | 1 | P (inherted from designation_code of statedesignation)
  1 | Member 2 | 1 | VP (inherted from designation_code of statedesignation)

statedesignation

  id | designation_code | designation_name | designation_desc

  1 | P | President | President Description. 
  1 | VP | Vice President | President Description. 

I would like to display the list of members followed by their designation:

I tried ordering the table but, I am not able to bring in the designation for the corresponding members,

My controller to bring in the Statemembers under the chairman

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

    But when I try this 
$statedesignation = Statemembers::find($id)->statedesignations()->get();
dd($statemembers);

    I am only getting Designation **President** since find($id) brings up only 1 or corresponding id 

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

       public function statemembers(){
        return $this->hasMany('App\Statemembers');
    }

Ref: This the continuation of this post



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire