-
Notifications
You must be signed in to change notification settings - Fork 906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting two fields or more as "attribute" in relations #348
Comments
Sure, just create an accesor on your User model, something like: public function getFullNameAttribute($value) {
return $this->firstname.' '.$this->name;
} You'll then be able to use Hope it helps. Cheers! |
Awesome. Thank you! |
For those who come accross this post and are in my situation: If you wan't to load the /**
* Appends to JSON
*
* @var string[]
*/
protected $appends = [
'full_name'
]; Otherwise the loaded data will not have this attribute in the response on frontend, and thus cannot display results in the select2 result box. |
Thanks @singeryo it's definitely helpful. And not only for ajax queries, for any scenario where you want to add something else to the model you should use Laravel model |
#define into user model
public function getFullNameAttribute($value) {
return $this->first_name.' '.$this->last_name;
}
#use in controller
$this->crud->addcolumn([
// role list
'label' => 'Primary program manager',
'name' => 'priorityProgramManger.full_name', // the method that defines the relationship in your Model
]); |
This is also helpful when you want to use relations with Ajax. # on Model
protected $appends = [
'full_title'
];
public function getFullTitleAttribute():string
{
return $this->company_number . ' - ' . $this->title;
} # on CrudController
CRUD::field('company')->type('relationship')->label('Şirket')->attribute('full_title')->ajax(true); In case someone (or me) needs it for later. |
Is it possible to concat two fields for the attribute (foreign key attribute that is shown to user) ?
For example i can display the name of the user with the relation below but i would like to display first + name.
Something like :
'attribute' => 'firstname' . 'name',
Thanks.
The text was updated successfully, but these errors were encountered: