Skip to content

Instantly share code, notes, and snippets.

@pxpm
Last active August 5, 2022 04:07
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/d584a109037d3efc7519872ac2096334 to your computer and use it in GitHub Desktop.
Save pxpm/d584a109037d3efc7519872ac2096334 to your computer and use it in GitHub Desktop.
Backpack 4.1 HasMany 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\Post::class);
CRUD::setRoute(config('backpack.base.route_prefix') . '/post');
CRUD::setEntityNameStrings('post', 'posts');
}
protected function setupCreateOperation()
{
CRUD::field('post_title');
CRUD::field('post_body');
CRUD::field('comments_list')
->type('repeatable')
->label('Comments')
->fields([
[
'name' => 'id',
'type' => 'hidden',
],
[
'name' => 'comment_text',
'type' => 'text',
],
]);
}
protected function setupUpdateOperation()
{
$this->setupCreateOperation();
}
public function store()
{
$items = collect(json_decode(request('comments_list'), true));
// create the main item as usual
$response = $this->traitStore();
// instead of returning, take a little time to create the post comments too
$post_id = $this->crud->entry->id;
// add the post_id to the items collection
$items->each(function($item, $key) use ($post_id) {
$item['post_id'] = $post_id;
\App\Models\Comment::create($item);
});
return $response;
};
public function update()
{
$items = collect(json_decode(request('comments_list'), true));
$response = $this->traitUpdate();
// instead of returning, take a little time to update the post comments too
$post_id = $this->crud->entry->id;
$created_ids = [];
$items->each(function($item, $key) use ($post_id, &$created_ids) {
$item['post_id'] = $post_id;
if ($item['id']) {
$comment = \App\Models\Comment::find($item['id']);
$comment->update($item);
} else {
$created_ids[] = \App\Models\Comment::create($item)->id;
}
});
// delete removed Comments
$related_items_in_request = collect(array_merge($items->where('id', '!=', '')->pluck('id')->toArray(), $created_ids));
$related_items_in_db = $this->crud->entry->comments;
$related_items_in_db->each(function($item, $key) use ($related_items_in_request) {
if (!$related_items_in_request->contains($item['id'])) {
$item->delete();
}
});
return $response;
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
use \Backpack\CRUD\app\Models\Traits\CrudTrait;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'post_id',
'comment_text',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
'post_id' => 'integer',
];
public function post()
{
return $this->belongsTo(\App\Models\Post::class);
}
}
<?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 = [
'post_title',
'post_body',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
];
public function comments()
{
return $this->hasMany(\App\Models\Comment::class);
}
//acessor to get the comments populated in the repeatable field again
public function getCommentsListAttribute() {
return json_encode($this->comments);
}
}
@maxou0789
Copy link

for everyone in the same situation I found a solution, force the language at the beginning of the setupCreateOperation() function:

app()->setlocale(request('locale')?:'en');

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