Backpack 4.1 morphOne 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 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; | |
} |
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 $fillable = ['url', 'description']; | |
/** | |
* Get the parent videoable model (user or post). | |
*/ | |
public function videoable() | |
{ | |
return $this->morphTo(); | |
} | |
} |
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; | |
/** | |
* 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]]); | |
} | |
} |
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 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]]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Same here - could you please add the
.php
extension to the filenames? 🙏