Skip to content
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

Closed
adbmdp opened this issue Jan 9, 2017 · 6 comments
Closed

Getting two fields or more as "attribute" in relations #348

adbmdp opened this issue Jan 9, 2017 · 6 comments

Comments

@adbmdp
Copy link
Contributor

adbmdp commented Jan 9, 2017

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.

$this->crud->addField([
            'label' => "User",
            'type' => 'select2_multiple',
            'name' => 'users',
            'entity' => 'users',
            'attribute' => 'name',
            'model' => "App\Models\User",
            'pivot' => true
]);

Something like :
'attribute' => 'firstname' . 'name',

Thanks.

@adbmdp adbmdp changed the title Getting two fields or more as "attribute" relations Getting two fields or more as "attribute" in relations Jan 9, 2017
@tabacitu
Copy link
Member

tabacitu commented Jan 9, 2017

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 full_name for the attribute, both in CRUD and anywhere else in your app ($user->full_name).

Hope it helps.

Cheers!

@tabacitu tabacitu closed this as completed Jan 9, 2017
@adbmdp
Copy link
Contributor Author

adbmdp commented Jan 9, 2017

Awesome. Thank you!

@singeryo
Copy link
Contributor

For those who come accross this post and are in my situation:

If you wan't to load the full_name attribute in ajax queries with a select2_ajax type field (for example), you need to add the attribute name to the $appends model attribute like so:

    /**
     * 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.

@pxpm
Copy link
Contributor

pxpm commented May 25, 2022

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 $appends .
Cheers

@kushsha
Copy link

kushsha commented Sep 13, 2022

 #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
        ]);

@altayevrim
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants