问题
I am trying to extend an update view to include a list view of some related items below the edit form.
I have two models, Publishers and Volumes, which have a many to many relationship. What I am trying to do is this.... when a user clicks on the edit button for a publisher, I want them to go to a page with the standard edit fields, but also have a list view below the form that lists all of the volumes that are connected to that publisher via their relationship.
Is there an easy way to do this?
I hope this makes sense.
回答1:
Indeed, there's no standard functionality to do that in Backpack. It's a pretty unusual way to do things. But it's not too difficult to achieve it.
If there aren't too many Vendors for one Publisher (as I expect it's the case here), I would keep it simple and NOT try to include the entire Backpack list view (with ajax, buttons, filters, etc) on top of the form. I would add a standard HTML table with the entries (and optionally buttons to Edit Vendor with target=_blank).
Here's how I would go about it:
In the Publisher CRUD, I would use a custom view for the Edit operation; you can do that using
$this->crud->setEditView('edit_publisher_with_vendors')
in yoursetup()
method;In that custom edit view (
edit_publisher_with_vendors.blade.php
in my example), I would copy-paste everything inside the edit.blade.php view that Backpack/CRUD is using, and add a table with the Vendors on top of the Edit form; notice you have the current entry as$entry
in this view; since there's a relationship on the model, you would be able to check if it has vendors using$entry->vendors()->count()
, and get the vendors using$entry->vendors
.
Hope it helps.
回答2:
As @tabacitu mentioned, Backpack doesn't currently have an built in solution for this. That said, this could maybe work for you:
This would allow you to use all functionality of the nested list view including interacting with the entities without conflicting at all with the parent
Step 1, Build your normal CRUDs
- Build out two normal CRUDs, one for Publishers, and one for Volumes
Step 2, Make a frameless layout
- copy
vendor/backpack/base/layout.blade.php
- name it
frameless-layout.blade.php
- remove
@include('backpack::inc.main_header')
and@include('backpack::inc.sidebar')
Step 3, Make a custom list view
- copy
vendor/backpack/crud/list.blade.php
- name it
sub-list.blade.php
- change the top line to
@extends('backpack::frameless-layout')
Step 4, Make a custom field
- Create a custom form field that contains an iFrame
- Inside your custom field template, have it set the url of the iFrame to the "list" url of the related resource
- You'd also need to utilize List Filters and a method for setting them dynamically so that the sub-list shows only the records related to the parent
Step 5, Configure and use the field
- In your crud controllers, use the
addField
to add the the configuration for the new field and its related model
来源:https://stackoverflow.com/questions/56977461/including-list-view-of-relationship-entity-on-update-page