Thanks for the great library! I posted earlier about how to integrate this with laravel-5-boilerplate, but I'm now considering rebuilding my project with backpack!
I've tried a couple of methods to nest resources that haven't been successful. I am trying to achieve a system where I can have URLs like this: /account/1/contact
This would be the CRUD index view of all the contacts tied to that particular account. I have achieved this so far using two separate resources, /account and /contact, which use n-n relationships. I also have filters on the /contact that allow me to specify an account to show.
Is there a way to write my routes in such a way that I can limit the scope of the nested resource to only show those attached to the first?
In short, yes, you can use the $this->crud->addClause() method, which allows you to modify the query. You'll find a few examples here, under Advanced Queries.
Also, here's a tutorial on how to achieve nested resources in Backpack\CRUD. Seems to me like you've already done 90% of that, you only need to add the query constraint.
Cheers!
P.S. I'll go ahead and close this issue so we don't clog the section. Feel free to open it again if you encounter problems or comment if you have follow-ups.
Thank you very much @tabacitu - I was very close, I was trying to use account/{account}/contact instead of dropping in the actual param.
;-)
Hey @tabacitu! I ran into an issue with this setup I wanted to point out. This works great for displaying nested resources, but fails when you try to hit any of the other routes on the controller (delete, showDetailsRow, edit). All of these controller methods only take one parameter for $id, but Laravel tries to pass them two because the route has two variables. It ends up passing the ID of the parent entity to the function. Example:
Route /account/2/contact/6/detailswould call to AccountContactsCrudController@showDetailsRow which accepts 1 parameter $id which should be 6, but instead is 2. The details returned are for the Contact entity with id 2. This also happens for delete or edit methods.
Since the AccountContactCrudController extends from ContactCrudController, you can't edit the method signature to take a second variable (and wouldn't want to manually overwrite all those methods anyway)
@jsiebach
Hi .... can you solve this issue .... I have the same problem now and no idea about that
@jsiebach - damn, you're right.
@M0H3N how about this for a fix - in the parent we overwrite all methods that don't work, and just call the child method, with the right parameter. This works for the tutorial example ("post" is the child entity - "user posts"):
class UserPostCrudController extends PostCrudController {
public function show($id)
{
return parent::show($this->request->post);
}
}
That should be a fix for you until we can find a long-term fix for this.
Here's all of them at this date:
public function show($id)
{
return parent::show($this->request->post);
}
public function listRevisions($id)
{
return parent::listRevisions($this->request->post);
}
public function restoreRevision($id)
{
return parent::restoreRevision($this->request->post);
}
public function showDetailsRow($id)
{
return parent::showDetailsRow($this->request->post);
}
Same issue. Waiting for a patch, I implemented the workaround through a Trait in the needed controllers.
My current controller, broken:
class ItemFieldCrudController extends CrudController
{
public function setup()
{
$project_id = \Route::current()->parameter('project_id');
$this->crud->setRoute(config('backpack.base.route_prefix') .'/'.$project_id. '/itemfield');
$this->crud->addClause('where', 'project_id', '=', $project_id);
This is the lines that break the edit, delete, and other operations in Backpack as the "project_id" will be viewed as the parameter to edit, delete etc.
Workaround:
In the controller:
use Traits\NestedRouteBadParameterBug;
And the Trait:
namespace App\Http\Controllers\Admin\Traits;
trait NestedRouteBadParameterBug
{
public function getLastParameter()
{
$params = \Route::current()->parameters();
$lastparam = array_values($params)[count($params)-1];
return $lastparam;
}
public function edit($id)
{
return parent::edit($this->getLastParameter());
}
public function view($id)
{
return parent::view($this->getLastParameter());
}
public function destroy($id)
{
return parent::destroy($this->getLastParameter());
}
// All the relevant functions of CrudController.php can be added.
}
Hope it will be patched soon!
@jrpub I see the issue is closed, but the problem still seems to exists?
Thank You @jrpub for the trait, You saved my day :)
Yes, the issue still exists. Too bad!
See #307 also, where the question left by @tabacitu stayed without any answer :-)
@jrpub , thanks a lot for your solution!
I've just implemented an official patch here: https://github.com/Laravel-Backpack/CRUD/pull/1339 , which I'll be merging after a bit more feedback & testing. Let me know what you think, and if it's a good fix for you guys. Cheers!
Most helpful comment
Same issue. Waiting for a patch, I implemented the workaround through a Trait in the needed controllers.
My current controller, broken:
This is the lines that break the edit, delete, and other operations in Backpack as the "project_id" will be viewed as the parameter to edit, delete etc.
Workaround:
In the controller:
use Traits\NestedRouteBadParameterBug;And the Trait:
Hope it will be patched soon!