首页 > 解决方案 > 将父数据传递到与 laravel 背包中的嵌套资源关联的列表或 crud

问题描述

我正在使用本教程https://backpackforlaravel.com/articles/tutorials/nested-resources-in-backpack-crud 在背包中设置嵌套资源。我意识到我可以自定义视图

$this->crud->setListView('my-view-name');

但我想知道如何将其他数据传递到此视图中。我想传递父资源,以便我可以说类似

Posts for {{$parent_resource->title}}

在列表(索引)视图的顶部。

所以我想我想知道是否有任何方法可以将父资源模型传递给列表和 crud 视图?

标签: laravel-backpack

解决方案


您应该能够编写类似的东西Posts for {{ $parent_resource->title }},但是从嵌套控制器,而不是视图。

在 Backpack 3.5 中,您可以使用以下方法更改操作的标题和副标题:

$this->crud->setTitle('some string', 'create'); // set the Title for the create action
$this->crud->setHeading('some string', 'create'); // set the Heading for the create action
$this->crud->setSubheading('some string', 'create'); // set the Subheading for the create action

现在,ChildCrudController::setup()您有请求,因此您可以:

  • 从中获取父 ID $this->crud->request
  • 从数据库中获取特定的父条目;
  • 使用上述方法设置标题(浏览器标题栏)、标题(页面上最大的文本)或副标题(页面上第二大文本,紧挨标题)的文本;

希望能帮助到你。干杯!


推荐阅读