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);
}
}
@gas4057
Copy link

gas4057 commented Oct 29, 2021

https://gist.github.com/pxpm/d584a109037d3efc7519872ac2096334#file-app-http-controllers-postcrudcontroller-L53

Good day! On this line I get error "Trying to get property 'id' of non-object". Have solutions?

@joseprigo
Copy link

Hi, I've followed this example and it works fine. It also improves de UX since the user doesn't need to navigate to the child element panel but I'm having some major issue:

I'm using SpatieTranslatable\HasTranslations from backpack and using this solution I get the [Object object ] value on the repeatable fields.

is there a way to parse the fields when defining the crud field?
thanks

@singeryo
Copy link

Hi !
Thanks for the example, this is great !

Only detail for me here is that this example doesn't fully take order into account (try to update a post by just reordering it's comments for exemple, you will see what I mean).
Here is a suggestion for those who need to be strict with order: simply don't put the hidden ID in the repeated fields. This way ALL items will be treated as creation and ALL previous items will be deleted (just keep in mind that the ID's will be changed, and there may be reasons why you wouldn't want that behaviour).

Also, if you wan't to keep your code as short as possible and don't care about dispatching deletion events, maybe you can delete the extra relation items this way ? (seems to work fine on my side):

// delete removed comments
$related_items_in_request = collect(array_merge($items->where('id', '!=', '')->pluck('id')->toArray(), $created_ids));
// Shorter way to handle the deletion, however keep in mind that this does not dispatch deletion events
Comment::where('post_id', '=', $post_id)->whereNotIn('id', $related_items_in_request)->delete();

@maxou0789
Copy link

@joseprigo Hi, I'm facing the same problem have you found any solution?
cheers

@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