[英]Laravel - Backpack select dependent from another select
我最近在我的 Laravel 項目中安裝了Laravel Backpack
admin。 目前我正在努力。 但我需要幫助。 所以...
我想要的是:
我想要兩個選擇,第一個是Category
,第二個是Article
。 所以Article
select 必須依賴於Category
select。 並且Article
belongsTo
Category
。
分類-文章
Category 1 = [ Article 1, Article 2, Article 3 ]
Category 2 = [ Article 4, Article 5 ]
這只是顯示哪個文章屬於類別。 因此,例如,當我在article
select 上單擊category
select 上的Category 1
時,它應該只顯示Article 1, Article 2 and Article 3
。
我做了什么
我按照 Backpack docs 上的說明添加了一個依賴於此鏈接Add a select2 field that depends on another field
。 所以我首先做了什么:
我創建了兩個表, Category
表和Article
表。 他們的模型:
class Category extends Model
{
use CrudTrait;
/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/
protected $table = 'categories';
protected $primaryKey = 'id';
// public $timestamps = false;
// protected $guarded = ['id'];
protected $fillable = ['title'];
// protected $hidden = [];
// protected $dates = [];
/*
|--------------------------------------------------------------------------
| FUNCTIONS
|--------------------------------------------------------------------------
*/
public function articles(){
return $this->hasMany('App\Models\Article');
}
}
這是一個Category
model,這是Article
model:
class Article extends Model
{
use CrudTrait;
/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/
protected $table = 'articles';
protected $primaryKey = 'id';
// public $timestamps = false;
// protected $guarded = ['id'];
protected $fillable = ['title', 'category_id'];
// protected $hidden = [];
// protected $dates = [];
/*
|--------------------------------------------------------------------------
| FUNCTIONS
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/
public function category(){
return $this->belongsTo('App\Models\Category');
}
}
我在這兩個模型之間建立了這樣的關系。 因此,當我創建Article
時,它會顯示標題,我必須從category select
中選擇category_id
。
畢竟,我制作了Archive
表,這些是我的遷移:
Schema::create('archives', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->bigInteger('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories');
$table->bigInteger('article_id')->unsigned();
$table->foreign('article_id')->references('id')->on('articles');
$table->timestamps();
});
還有我的Archive.php
model:
class Archive extends Model
{
use CrudTrait;
/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/
protected $table = 'archives';
protected $primaryKey = 'id';
// public $timestamps = false;
// protected $guarded = ['id'];
protected $fillable = ['title', 'category', 'article'];
// protected $hidden = [];
// protected $dates = [];
/*
|--------------------------------------------------------------------------
| FUNCTIONS
|--------------------------------------------------------------------------
*/
/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/
public function category(){
return $this->belongsTo('App\Models\Category');
}
public function article(){
return $this->belongsTo('App\Models\Article');
}
}
然后我按照backpack docs
的說明進行操作。 這是我的ArchiveCrudController
:
public function setup()
{
/*
|--------------------------------------------------------------------------
| CrudPanel Basic Information
|--------------------------------------------------------------------------
*/
$this->crud->setModel('App\Models\Archive');
$this->crud->setRoute(config('backpack.base.route_prefix') . '/archive');
$this->crud->setEntityNameStrings('archive', 'archives');
$this->crud->setColumns(['title', 'category', 'article']);
$this->crud->addField([
'name' => 'title',
'type' => 'text',
'label' => "Archive title"
]);
$this->crud->addField([ // SELECT2
'label' => 'Category',
'type' => 'select',
'name' => 'category_id',
'entity' => 'category',
'attribute' => 'title',
]);
$this->crud->addField([ // select2_from_ajax: 1-n relationship
'label' => "Article", // Table column heading
'type' => 'select2_from_ajax',
'name' => 'article_id', // the column that contains the ID of that connected entity;
'entity' => 'article', // the method that defines the relationship in your Model
'attribute' => 'title', // foreign key attribute that is shown to user
'data_source' => url('api/article'), // url to controller search function (with /{id} should return model)
'placeholder' => 'Select an article', // placeholder for the select
'minimum_input_length' => 0, // minimum characters to type before querying results
'dependencies' => ['category'], // when a dependency changes, this select2 is reset to null
//'method' => ‘GET’, // optional - HTTP method to use for the AJAX call (GET, POST)
]);
/*
|--------------------------------------------------------------------------
| CrudPanel Configuration
|--------------------------------------------------------------------------
*/
// TODO: remove setFromDb() and manually define Fields and Columns
//$this->crud->setFromDb();
// add asterisk for fields that are required in ArchiveRequest
$this->crud->setRequiredFields(StoreRequest::class, 'create');
$this->crud->setRequiredFields(UpdateRequest::class, 'edit');
}
就像來自backpack docs
一樣。 然后我在App\Http\Controller
中創建了Api
文件夾,然后在其中創建了ArticleController
,如下所示:
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Article;
use Illuminate\Http\Request;
class ArticleController extends Controller
{
public function index(Request $request)
{
$search_term = $request->input('q');
$form = collect($request->input('form'))->pluck('value', 'name');
$options = Article::query();
// if no category has been selected, show no options
if (! $form['category']) {
return [];
}
// if a category has been selected, only show articles in that category
if ($form['category']) {
$options = $options->where('category_id', $form['category']);
}
if ($search_term) {
$results = $options->where('title', 'LIKE', '%'.$search_term.'%')->paginate(10);
} else {
$results = $options->paginate(10);
}
return $options->paginate(10);
}
public function show($id)
{
return Article::find($id);
}
}
我只是從docs
中復制粘貼代碼,但當然我根據需要更改了 model。 最后是我的routes
:
Route::get('api/article', 'App\Http\Controllers\Api\ArticleController@index');
Route::get('api/article/{id}', 'App\Http\Controllers\Api\ArticleController@show');
我將這些路線復制粘貼到我的web.php
的routes
文件夾和custom.php
的routes/backpack
文件夾中。
但是當我在我的Archive create
任何articles
顯示。 有人可以幫我嗎?
從背包 4.1 開始,您應該在依賴字段上包含 'include_all_form_fields' => true 屬性。
您的代碼中有一些問題,我希望這可以解決它們:
1- 在 ArchiveCrudController 中:確保具有 category_id 的依賴項不是類別...
'dependencies' => ['category_id'],
$this->crud->addField([ // select2_from_ajax: 1-n relationship
'label' => "Article",
'type' => 'select2_from_ajax',
'name' => 'article_id',
'entity' => 'article',
'attribute' => 'title',
'data_source' => url('api/article'),
'placeholder' => 'Select an article',
'minimum_input_length' => 0, querying results
'dependencies' => ['category_id'],
//'method' => ‘GET’,
]);
2-在存檔 Model $fillable 應該采用 db 列名而不是關系...
protected $fillable = ['title', 'category_id', 'article_id'];
3- 當你得到 $form 請求參數時,它帶有名稱 category_id 就像你在 Crud 中命名它不是(類別)
public function index(Request $request)
{
$search_term = $request->input('q');
$form = collect($request->input('form'))->pluck('value', 'name');
$options = Article::query();
// if no category has been selected, show no options
if (! $form['category_id']) {
return [];
}
// if a category has been selected, only show articles in that category
if ($form['category_id']) {
$options = $options->where('category_id', $form['category_id']);
}
if ($search_term) {
$results = $options->where('title', 'LIKE', '%'.$search_term.'%')->paginate(10);
} else {
$results = $options->paginate(10);
}
return $options->paginate(10);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.