I have a model, which is storing foreign key data of various tables. Here is the model details:
class InstituteStage extends Model
{
    protected $guarded = [];
    public function branch()
    {
        return $this->belongsTo('App\Model\Branch');
    }
    public function version()
    {
        return $this->belongsTo('App\Model\Version');
    }
    public function shift()
    {
        return $this->belongsTo('App\Model\Shift');
    }
    public function stage()
    {
        return $this->belongsTo('App\Model\Stage');
    }
    public function academic_year()
    {
        return $this->belongsTo('App\Model\AcademicYear');
    }
}
Right now if i fetch the data, i'm getting those fields:
Array
(
    [0] => Array
        (
            [id] => 3
            [stage_for] => Teachers
            [branch_id] => 1
            [version_id] => 1
            [shift_id] => 1
            [stage_id] => 1
            [academic_year_id] => 1
        )
    [1] => Array
        (
            [id] => 4
            [stage_for] => Students
            [branch_id] => 2
            [version_id] => 2
            [shift_id] => 2
            [stage_id] => 2
            [academic_year_id] => 2
        )
)
Now i want the details (such as name) of other tables. The response i'm looking for is:
Array
(
    [0] => Array
        (
            [id] => 3
            [stage_for] => Teachers
            [institute_stage_name] => "branch_name->version_name->shift_name->stage_name" 
        )
    [1] => Array
        (
            [id] => 4
            [stage_for] => Students
            [institute_stage_name] => "branch_name->version_name->shift_name->stage_name" 
        )
)
I can get datas using eager loading and concat them in frontend. But i want to use a custom model which will return this details. Is it possible to get this structure!
i made another static method in my model and getting all the data:
public static function student_institute_stage_data()
{
    return static::where([
        ['stage_for', 'Students'],
        ['status', 'active']
    ])
    ->with(['branch', 'version', 'shift', 'stage'])
    ->get();
}
then in my controller:
$instituteStage = InstituteStage::student_institute_stage_data();
after that in my blade, im doing this ->
@foreach ($instituteStage as $stage)
    <option value=""  ></option>
@endforeach
via Chebli Mohamed
 
Aucun commentaire:
Enregistrer un commentaire