Skip to content

Instantly share code, notes, and snippets.

@pxpm
Last active October 11, 2021 12:25
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/c49c4ebb3c8d8af8a5bcce3122622a61 to your computer and use it in GitHub Desktop.
Save pxpm/c49c4ebb3c8d8af8a5bcce3122622a61 to your computer and use it in GitHub Desktop.
Backpack 4.1 morphOne relation
<?php
namespace App\Http\Controllers;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
class UserCrudController extends CrudController
{
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
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
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;
public function setup()
{
CRUD::setModel(\App\Models\User::class);
CRUD::setRoute(config('backpack.base.route_prefix') . '/user');
CRUD::setEntityNameStrings('user', 'users');
}
protected function setupListOperation()
{
CRUD::column('name');
}
protected function setupCreateOperation()
{
CRUD::field('name');
CRUD::field('video_field')
->type('repeatable')
->fields([
[
'name' => 'url',
'type' => 'text',
],
[
'name' => 'description',
'type' => 'text'
]])
->max_rows(1)
->min_rows(1)
]);
}
protected function setupUpdateOperation()
{
$this->setupCreateOperation();
}
protected function setupShowOperation()
{
CRUD::column('name');
}
public function store()
{
$video = json_decode(request('video_field'), true)[0];
$response = $this->traitStore();
$this->crud->entry->video()->create($video);
return $response;
};
public function update()
{
$video = json_decode(request('video_field'), true)[0];
$response = $this->traitUpdate();
$this->crud->entry->video()->update($video);
return $response;
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Video extends Model
{
use \Backpack\CRUD\app\Models\Traits\CrudTrait;
protected $fillable = ['url', 'description'];
/**
* Get the parent videoable model (user or post).
*/
public function videoable()
{
return $this->morphTo();
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use \Backpack\CRUD\app\Models\Traits\CrudTrait;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
];
public function video()
{
return $this->morphOne(\App\Models\Video::class, 'videoable');
}
//acessor to get the video populated in the repeatable field again
public function getVideoFieldAttribute() {
return json_encode([['url' => $this->video->url, 'description' => $this->video->description]]);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use \Backpack\CRUD\app\Models\Traits\CrudTrait;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
];
public function video()
{
return $this->morphOne(\App\Models\Video::class, 'videoable');
}
//acessor to get the video populated in the repeatable field again
public function getVideoFieldAttribute() {
return json_encode([['url' => $this->video->url, 'description' => $this->video->description]]);
}
}
@tabacitu
Copy link

Same here - could you please add the .php extension to the filenames? 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment