I've encountered a strange bug/issue in my opinion while updating a (goal) model using the BackpackCrudController.
First let me give you some information about my database structure and models. (i've left out useless relations and functions)
quick grasp from my composer.json
"php": "^7.2",
"backpack/crud": "4.0.*",
"backpack/logmanager": "^3.0",
"backpack/pagemanager": "^2.0",
"backpack/permissionmanager": "^5.0",
"fideloper/proxy": "^4.0",
"laravel/framework": "^6.2",
"laravel/tinker": "^2.0"
recipes table:
Schema::create('recipes', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('slug');
$table->string('image');
$table->text('content');
$table->text('preparation');
$table->json('ingredients');
$table->integer( 'goal_id')->unsigned()->nullable();
$table->integer( 'category_id')->unsigned()->nullable();
$table->unsignedBigInteger( 'user_id');
$table->timestamps();
});
Schema::table('recipes', function(Blueprint $table) {
$table->foreign( 'goal_id')->references( 'id')->on('goals');
$table->foreign( 'category_id')->references( 'id')->on('categories');
$table->foreign( 'user_id')->references( 'id')->on('users');
});
goals table:
Schema::create('goals', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('slug');
$table->string('image');
$table->text( 'content');
$table->string('workbook')->nullable();
$table->tinyInteger('duration')->nullable();
$table->timestamps();
});
Recipe model:
class Recipe extends Model
{
use CrudTrait;
use Sluggable;
use SluggableScopeHelpers;
/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/
protected $table = 'recipes';
protected $guarded = ['id'];
protected $fillable = ['title', 'slug', 'image', 'content', 'preparation', 'ingredients', 'goal_id', 'category_id', 'user_id'];
/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/
public function goal( ) {
return $this->belongsTo( Goal::class);
}
}
Goal model:
class Goal extends Model
{
use CrudTrait;
use Sluggable;
use SluggableScopeHelpers;
/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/
protected $table = 'goals';
protected $guarded = ['id'];
protected $fillable = ['title', 'slug', 'image', 'content', 'workbook', 'duration'];
/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/
public function recipes( ) {
return $this->hasMany( Recipe::class,'goal_id', 'id');
}
}
The problem exists while updating a goal, so here's my GoalCrudController:
class GoalCrudController extends CrudController
{
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;
public function setup()
{
$this->crud->setModel('App\Models\Goal');
$this->crud->setRoute(config('backpack.base.route_prefix') . '/goal');
$this->crud->setEntityNameStrings('goal', 'goals');
}
protected function setupCreateOperation()
{
$this->crud->setValidation(GoalRequest::class);
$this->crud->addField([
'label' => 'Articles',
'type' => 'select2_multiple',
'name' => 'articles',
'entity' => 'articles',
'attribute' => 'title',
]);
$this->crud->addField([
'label' => 'Recipes',
'type' => 'select2_multiple',
'name' => 'recipes',
'entity' => 'recipes',
'attribute' => 'title',
]);
}
protected function setupUpdateOperation()
{
$this->setupCreateOperation();
$this->crud->removeField('image');
$this->crud->addField([
'name' => 'image',
'label' => 'Image',
'type' => 'image',
'upload' => true,
'prefix' => 'uploads/',
])->afterField('title');
}
}
Again, i left out functions that are not for this issue.
Now, the problem is when i create a goal, i can attach a recipe (from the select2_multiple field) and the relation between a goal and a recipe will be created. Though, when i want to edit/update the goal i just created, to add a new recipe for example, i get the following Exception:
Exception
Property [recipes] does not exist on this collection instance.
I've tried to edit the field in the setupUpdateOperation()
to the following:
$this->crud->removeField('recipes');
$this->crud->addField([
'label' => 'Recipes',
'type' => 'select2_multiple',
'name' => 'recipes',
'entity' => 'recipes',
'attribute' => 'title',
'value' => Recipe::where('goal_id', $this->crud->getCurrentEntryId()), // <-- added this
]);
But then i get the following Exception:
Call to a member function pluck() on string (View: /Users/name/Docker/sitename/vendor/backpack/crud/src/resources/views/crud/fields/select_multiple.blade.php) // <-- at line 27
My questions now are actually:
If i need to provide more information, please let me know and i'll be happy to provide it for you. Thanks in advance!
PS. The issue is the same when using select_multiple.
After fiddling around a bit, i managed to find a solution.
In the setupUpdateOperation
function i added the following:
$this->crud->removeField('recipes'); // remove the original field
$this->crud->addField([
'label' => 'Recipes',
'type' => 'select2_multiple',
'name' => 'recipes',
'entity' => 'recipes',
'attribute' => 'title',
'value' => $this->crud->getCurrentEntry()->recipes,
]);
With this i managed to get the currently related recipes in the update view and actually update the related recipes.