[英]Laravel backpack select_multiple | select2_multple breaks while updating/editing with one to many relation
I've encountered a strange bug/issue in my opinion while updating a (goal) model using the BackpackCrudController.我在使用 BackpackCrudController 更新(目标)model 时遇到了一个奇怪的错误/问题。
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从我的 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:配方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:目标 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:更新目标时存在问题,所以这是我的 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.现在,问题是当我创建一个目标时,我可以附加一个配方(来自 select2_multiple 字段)并且将创建目标和配方之间的关系。 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:我尝试将
setupUpdateOperation()
中的字段编辑为以下内容:
$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.
使用 select_multiple 时问题相同。
After fiddling around a bit, i managed to find a solution.摆弄了一下之后,我设法找到了解决方案。
In the setupUpdateOperation
function i added the following:在
setupUpdateOperation
function 中,我添加了以下内容:
$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.有了这个,我设法在更新视图中获取当前相关的食谱,并实际更新相关的食谱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.