Skip to content

Instantly share code, notes, and snippets.

@pxpm
Created October 8, 2021 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pxpm/2a45de69d755f673e3fc5fa60b2b0441 to your computer and use it in GitHub Desktop.
Save pxpm/2a45de69d755f673e3fc5fa60b2b0441 to your computer and use it in GitHub Desktop.
Backpack 4.1 morphToMany relation
<?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;
}
<?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];
}));
}
}
<?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');
}
}
<?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