I use backpack 5 Pro on last laravel.
I would like to select several data, save them in the database and retrieve them.
I have two tables :
products - id, name
articles - id, title, products_id
In my model Articles.php :
public function produit()
{
return $this->belongsTo('App\Models\Products', 'products_id', 'id');
}
In my controller ArticlesCrudController.php :
$this->crud->addField(
[
'label' => "Products Links",
'type' => 'relationship',
'name' => 'produit',
'entity' => 'produit',
'model' => "App\Models\Products",
'attribute' => 'product_name',
'allows_null' => true,
'multiple' => true, <--- WANT MULTIPLE SELECT
'tab' => 'Products links',
'options' => (function ($query) {
return $query->orderBy('product_name', 'ASC')->get();
}),
]
);
In my Database :
products_id contains : ["15","18"]
Everything works fine except in my results list. It only shows me one result (id : 15) instead of two or more... (15 and 18).
CodePudding user response:
This is not how you can make such relation, you can store only one value within product_id
.
Based on your structure, your Product can have multiple Articles, so create two lines (two records in DB) in your articles:
Consider this pseudo code:
$article1 = [
'id' => 1,
'title' => 'Article 1',
'product_id' => 15
];
$article2 = [
'id' => 1,
'title' => 'Article 1',
'product_id' => 15
]
Now Product with ID of 15 has 2 articles (1 and 2).
If you want your Article to have multiple Products and vice-versa then create Many to many relation