如何在自定义非CRUD页面使用Laravel Backpack字段时传递获取模型值
非CRUD页面向Laravel Backpack字段传递Model值的实现方式
直接按以下步骤操作即可完成值绑定:
- 第一步:获取你需要填充的目标Model实例
示例代码:// 替换为实际的Model类和查询逻辑 $targetModel = \App\Models\Post::find($postId); - 第二步:将实例绑定到CRUD面板的entry属性
在定义字段的控制器方法中,完成CRUD基础配置后做核心赋值即可:// 先完成CRUD基础初始化 $this->crud->setModel(\App\Models\Post::class); $this->crud->setRoute('your-custom-page-route'); $this->crud->setEntityNameStrings('单条目名称', '多条目名称'); // 核心绑定逻辑,Backpack会自动从这个属性读取字段值 $this->crud->entry = $targetModel; - 第三步:正常定义字段即可
不需要额外给字段传value参数,框架会自动读取entry里的对应属性填充字段:$this->crud->addField([ 'name' => 'title', 'type' => 'text', 'label' => '标题' ]); $this->crud->addField([ 'name' => 'content', 'type' => 'ckeditor', 'label' => '内容' ]);
如果需要手动覆盖个别字段的值,可以直接在字段定义中加value参数:
$this->crud->addField([ 'name' => 'custom_tip', 'type' => 'text', 'label' => '自定义提示', 'value' => '你自定义的特殊值' ]);
如果是新增场景没有对应数据,直接给entry赋值空的Model实例即可:$this->crud->entry = new \App\Models\Post();
内容的提问来源于stack exchange,提问作者Gustavo Silva
相关产品推荐
相关产品推荐

