One to many relationship

Posted 9 months ago by kshitizmittal

I have two tables "departments" and "designations", "departments" table is parent table which has many "designations"

Department Model :-

class Department extends Model { protected $primaryKey = 'deptid'; protected $fillable = ['deptname','deptparent','deptcode','deptcomments','deptstatus'];

public function designations()
{
    return $this->hasMany('App\Designation','deptparent');
}

}

Designation Model :-

class Designation extends Model { protected $primaryKey = 'degid';

protected $fillable = ['deptparent','degname','degcode','degstatus','degdescription'];

public function departments()
{
    return $this->belongsTo('App\Department');
}

}

foreign key in "designations" table "deptparent" references "deptid" primary key in "departments" table.

Now on blade page,

I want all designations name with their corresponding parent department name

Controller :-

public function index() { $designations = Designation::with('departments')->get(); return view('eno.designation',compact('designations')); }

HOW TO REFER PARENT DEPARTMENT and SHOW CORRESPONDING PARENT DEPARTMENT NAME ?

Please sign in or create an account to participate in this conversation.