You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BackPack CRUD实现选择学校名称保存ID的关联配置咨询

Solution for Configuring People CRUD with School Association in Backpack CRUD PRO

Step 1: Define Model Relationships

Ensure your Person and School models have the correct Eloquent relationships:

In app/Models/Person.php:

public function school()
{
    return $this->belongsTo(School::class);
}

In app/Models/School.php:

public function people()
{
    return $this->hasMany(Person::class);
}

Also, add school_id to the $fillable array in Person.php:

protected $fillable = [..., 'school_id'];

Step 2: Configure List View Columns

In your PersonCrudController.php, add a column to display the associated school name in the list view:

public function setupListOperation()
{
    // ... other columns
    $this->crud->addColumn([
        'name' => 'school.name', // Use the relationship attribute to fetch the name
        'label' => 'School Name',
        'type' => 'text',
    ]);
}

Step 3: Configure Searchable Select Field for Edit/Create

To add a searchable school selector that saves only the school_id to the database, use the select2_from_ajax field (ideal for large datasets):

3.1 Add the Field to Create/Update Operations

In PersonCrudController.php:

public function setupCreateOperation()
{
    $this->setupFields();
}

public function setupUpdateOperation()
{
    $this->setupFields();
}

private function setupFields()
{
    // ... other fields
    $this->crud->addField([
        'name' => 'school_id',
        'label' => 'School',
        'type' => 'select2_from_ajax',
        'entity' => 'school', // Matches the relationship name in Person model
        'attribute' => 'name', // The school attribute to display in the select
        'data_source' => url('admin/school/ajax-search'), // Route to fetch search results
        'placeholder' => 'Select a school',
        'minimum_input_length' => 1, // Start searching after 1 character is typed
    ]);
}

3.2 Create the AJAX Search Route

Add this route to routes/backpack/custom.php:

use App\Http\Controllers\Admin\SchoolCrudController;

Route::get('admin/school/ajax-search', [SchoolCrudController::class, 'ajaxSearch'])->name('school.ajax-search');

3.3 Implement the AJAX Search Method

In SchoolCrudController.php, add the method to return filtered school results:

public function ajaxSearch()
{
    $searchTerm = request()->input('q');
    $schools = School::where('name', 'LIKE', "%{$searchTerm}%")
        ->select('id', 'name as text') // Format required by select2
        ->get();
    
    return response()->json(['results' => $schools]);
}

Alternative for Small School Lists

If your school list is small, you can use the simpler select2 field instead of AJAX:

$this->crud->addField([
    'name' => 'school_id',
    'label' => 'School',
    'type' => 'select2',
    'entity' => 'school',
    'attribute' => 'name',
    'model' => \App\Models\School::class,
]);

内容的提问来源于stack exchange,提问作者Rainnnyyyy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 17:50:36