I have a subfield relationship that I need to show using an accessor attribute: reverse_full_name. This works fine, but I need to add a scopeIsActive clause to limit the options for selection. When I add an options => function as shown in the code, I get MySQL error Unknown column ‘reverse_full_name’ in ‘field list’. If I add a DB::raw() to concatenate the names and alias it as reverse_full_name, the clause gets applied but the select options have the user id but a blank label.
I can get it to work perfectly if I use last_name as the attribute but not with the accessor attribute.
I have also tried using ajax fetch which always returns no results. Again, using last_name as the attribute works.
I have a Centre model that hasMany Allocations. The allocation belongsTo a User.
In the CentreCrudController you can add Allocations and in the Allocation subfields you select the User to assign the Allocation to.
Here is the input and the selected but blank labelled current User.
Here is the relevant code.
// app/Http/Controllers/Admin/CentreCrudController.php
class CentreCrudController extends CrudController
{
protected function setupCreateOperation()
{
CRUD::field('allocations')
->type('relationship')
->subfields([
[
'name' => 'user',
'options' => (function ($query) {
return $query->select([
'users.*',
DB::raw("concat(last_name, ' ', first_name) as reverse_full_name")
])
->active()
->orderBy('reverse_full_name');
}),
'attribute' => 'reverse_full_name'
],
[
'name' => 'valid_date',
'type' => 'date'
],
]);
}
}
// app/Models/User.php
class User extends Authenticatable
{
protected $appends = [
'reverse_full_name',
];
public function identifiableAttribute()
{
return 'reverse_full_name';
}
public function getReverseFullNameAttribute()
{
return "{$this->last_name} {$this->first_name}";
}
public function allocations(): HasMany
{
return $this->hasMany(Allocation::class);
}
}
// app/Models/Centre.php
class Centre extends Model
{
public function allocations(): HasMany
{
return $this->hasMany(Allocation::class);
}
}
// app/Models/Allocation.php
class Allocation extends Model
{
public function centre(): BelongsTo
{
return $this->belongsTo(Centre::class);
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}
I would suggest to don’t use relationships inside relationships fields, probably making your field a select2 field works best in your scenario:
[
'name' => 'user_id',
'entity' => false, // depending on some factors, may be a good idea to force a non-relationship field.
'type' => 'select2',
'model' => AppModelsUser::class
'options' => fn($query) => $query->active()->get(),
'attribute' => 'reverse_full_name',
]
It should work like that, if not let me know.
You can also open a discussion in our https://github.com/Laravel-Backpack/community-forum/ or search for potential answers to your questions there.
Cheers
1
I eventually found the doumentation for how to override the fetchRelation method.
I did have to use the separate name parts in the searchable_attributes but I still get to display it to the user with the reverse_full_name.
I think ajax is the way to go as there could be a lot of users to prefill the dropdown with.
I like Pedro’s solution too to make it a select_2 instead of a relation, and can present both options to the client.
public function fetchUser() {
return $this->fetch([
'model' => User::class,
'searchable_attributes' => ['first_name', 'last_name'],
'searchOperator' => 'LIKE',
'query' => function($model) {
return $model->active()->orderBy('last_name');
}
]);
}