lundi 7 mars 2016

Laravel: serverfireteam/panel Display foreign key value instead of ID on Grid

Im currently working a Laravel project that uses serverfireteam/panel. Im trying to figure out a way to display a foreign key object value instead of the id. Currently I have a StudentController to display student information and have no problem displaying the grid and information with:

$this->grid->add('field','label').

But one of my fields is a foreign key that comes from another table called orientation_dates so I can add it no prob as follows:

$this->grid->add('orientation_date_id', 'Date Attending');

but of course this shows the id number. What i want is to display the date column from this foreign object.

I tried tacking date(column name) to it:

$this->grid->add('orientation_date_id.date', 'Date Attending');

but this returns the grid with it displaying {{$orientation_date_id->date}}

I have a function orientation_date with the hasOne relationship function in the Student model:

Im still fairly new to Laravel so im not exactly sure what im missing.:(( I'd appreciate any help or guidance you may offer!:)

Here is an image to clarify some more of what i want and here is a the code for my controller:

class StudentController extends CrudController{

public function all($entity){
    parent::all($entity); 

        $this->filter = \DataFilter::source(new \App\Student());
        $this->filter->add('student_id', 'Student ID', 'text');
        $this->filter->submit('search');
        $this->filter->reset('reset');
        $this->filter->build();

        $this->grid = \DataGrid::source($this->filter);

        $this->grid->add('student_id', 'Student ID');
        $this->grid->add('first_name', 'First Name',true);
        $this->grid->add('last_name', 'Last Name',true);
        $this->grid->add('orientation_date_id', 'Date Attending');//<-here
        $this->grid->orderBy('order','asc'); 

        $this->addStylesToGrid();



        return $this->returnView();
}

public function  edit($entity){

    parent::edit($entity);

        $this->edit = \DataEdit::source(new \App\Student());

        $this->edit->label('Student Information');

        $this->edit->add('orientation_date_id', 'Date Attending', 'select')->options(\App\OrientationDate::lists("date", "id")->all())->rule('required');

        return $this->returnEditView();
}    

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire