Backpack 4.1 morphToMany relation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Backpack\CRUD\app\Http\Controllers\CrudController; | |
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD; | |
class PostCrudController extends CrudController | |
{ | |
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation { store as traitStore; } //IMPORTANT HERE | |
use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation { update as traitUpdate; } //IMPORTANT HERE | |
public function setup() | |
{ | |
CRUD::setModel(\App\Models\Article::class); | |
CRUD::setRoute(config('backpack.base.route_prefix') . '/article'); | |
CRUD::setEntityNameStrings('article', 'articles'); | |
} | |
protected function setupCreateOperation() | |
{ | |
CRUD::field('title'); | |
CRUD::field('tags_list') | |
->type('repeatable') | |
->fields([ | |
[ | |
'name' => 'tag_id', | |
'type' => 'select2', | |
'model' => 'App\Models\Tag, | |
'attribute' => 'name' | |
], | |
[ | |
'name' => 'note', | |
'type' => 'text' | |
]]) | |
]); | |
} | |
protected function setupUpdateOperation() | |
{ | |
$this->setupCreateOperation(); | |
} | |
public function store() | |
{ | |
$tags = collect(json_decode(request('tags_list'), true)); | |
$response = $this->traitStore(); | |
$this->crud->entry->tags()->sync($tags); | |
return $response; | |
}; | |
public function update() | |
{ | |
$tags = collect(json_decode(request('tags_list'), true)); | |
$response = $this->traitUpdate(); | |
$this->crud->entry->tags()->sync($tags); | |
return $response; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Model; | |
class Post extends Model | |
{ | |
use \Backpack\CRUD\app\Models\Traits\CrudTrait; | |
protected $casts = [ | |
'id' => 'integer', | |
]; | |
public function tags() | |
{ | |
return $this->morphToMany(\App\Models\Tag::class, 'taggable')->withPivot('note'); | |
} | |
//acessor to get the video populated in the repeatable field again | |
public function getTagsListAttribute() { | |
return json_encode($this->tags->map(function($tag) { | |
return ['tag_id' => $tag->id, 'note' => $tag->pivot->note]; | |
})); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Model; | |
class Tag extends Model | |
{ | |
use \Backpack\CRUD\app\Models\Traits\CrudTrait; | |
public function posts() | |
{ | |
return $this->morphedByMany(\App\Models\Post::class, 'taggable'); | |
} | |
public function videos() | |
{ | |
return $this->morphedByMany(\App\Models\Video::class, 'taggable'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Model; | |
class Video extends Model | |
{ | |
use \Backpack\CRUD\app\Models\Traits\CrudTrait; | |
protected $casts = [ | |
'id' => 'integer', | |
]; | |
public function tags() | |
{ | |
return $this->morphToMany(\App\Models\Tag::class, 'taggable')->withPivot('note'); | |
} | |
//acessor to get the video populated in the repeatable field again | |
public function getTagsListAttribute() { | |
return json_encode($this->tags->map(function($tag) { | |
return ['tag_id' => $tag->id, 'note' => $tag->pivot->note]; | |
})); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment