我认为使用BackpackCrudController更新(目标)模型时遇到了一个奇怪的错误/问题。
首先,我给您一些有关我的数据库结构和模型的信息。 (我忽略了无用的关系和功能)
快速了解我的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"
配方表:
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');
});
目标表:
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();
});
配方模型:
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);
}
}
目标模式:
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');
}
}
更新目标时存在问题,所以这是我的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');
}
}
同样,我忽略了不适用于此问题的功能。
现在,问题是当我创建目标时,我可以附加一个配方(来自select2_multiple字段),并且将创建目标和配方之间的关系。但是,当我要编辑/更新刚创建的目标时,例如要添加新配方,我会收到以下异常:
Exception
Property [recipes] does not exist on this collection instance.
我已经尝试将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
]);
但是后来我得到以下异常:
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
我的问题实际上是:
如果我需要提供更多信息,请告诉我,我们很乐意为您提供信息。预先感谢!
PS。使用select_multiple时,问题相同。
经过一番摆弄之后,我设法找到了解决方法。
在setupUpdateOperation
功能中,我添加了以下内容:
$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,
]);
通过此操作,我设法在更新视图中获取了当前相关的食谱,并实际上更新了相关的食谱。