Laravel 8.* + Backpack + CRUD- Article with tags

php artisan make:migration:schema create_article_tags_table --model=0 --schema="article_id:integer:unsigned,tag_id:integer:unsigned"
Create Article Tags migration
php artisan migrate
php artisan make:migration:schema create_articles_table --model=0 --schema="title:string,content:longText"
php artisan migrate
php artisan backpack:crud article
public function tags()
{
return $this->belongsToMany(Tag::class, 'article_tags','article_id', 'tag_id');
}
private function getFieldsData($show = FALSE) {
return [
[
'name'=> 'title',
'label' => 'Title',
'type'=> 'text'
],
[
'name' => 'content',
'label' => 'Content',
'type' => ($show ? "textarea": 'ckeditor'),
],
[ // Select2Multiple = n-n relationship (with pivot table)
'label' => "Tags",
'type' => ($show ? "select": 'select2_multiple'),
'name' => 'tags', // the method that defines the relationship in your Model
// optional
'entity' => 'tags', // the method that defines the relationship in your Model
'model' => "App\Models\Tag", // foreign key model
'attribute' => 'name', // foreign key attribute that is shown to user
'pivot' => true, // on create&update, do you need to add/delete pivot table entries?
]
];
}
protected function setupShowOperation()
{
// by default the Show operation will try to show all columns in the db table,
// but we can easily take over, and have full control of what columns are shown,
// by changing this config for the Show operation
$this->crud->set('show.setFromDb', false);
$this->crud->addColumns($this->getFieldsData(TRUE));
}
Menu is updated
Listing is present
Add is updated
Preview is updated

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store