How to add a custom clause to a subfield relationship with an accessor attribute in Backpack for Laravel Version 6?

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');
        } 
    ]);
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật