Edit2: The problem is solved now thanks to @tabacitu. Thanks to everyone who tried to help! Please refer to this github thread for further information while I update the question: https://github.com/Laravel-Backpack/CRUD/issues/2049
Edit: I have tried this on a completely fresh backpack installation using only the User
model. Same problem.
Added the following route at the and of routes/backpack/custom.php
:
Route::get('test/{id}', 'UserCrudController@index');
This is my URL: https://example.com/admin/test/30
Then added the following code to my UserCrudController.php
:
$id = \Route::current()->parameter('id');
Now again, this doesn't work:
$this->crud->addClause('where','id', "=", $id); //produces empty result, but $id contains 30
But this does work:
$this->crud->addClause('where','id', "=", 30); //here I get my user with id 30.
I have an Article
model and am using $this->crud->addClause('where','user_id',backpack_user()->id);
to limit the articles shown to those belonging to the currently logged in user.
I now want to further filter my articles by only showing articles that have a certain page_id
- in fact, I can do this without any hassle:
$this->crud->addClause('where','page_id', "=", "154");
Now only articles that have a page_id
of 154 are shown.
Obviously I want a dynamic variable for page_id, so my url looks like this
http://example.com/admin/articles/154
and I extract the last parameter like this:
$segments = request()->segments();
$page_id = $segments[count($segments)-1]; //get last segment, contains "154"
Now my problem is that for some obscure reason, whenever I try to use
$this->crud->addClause('where','page_id', "=", $page_id);
The collection is empty, even tho dd()'ing $page_id
shows it is set and contains "154". I have even tried to cast $page_id to int like this and in fact it becomes an int but still the same problem (empty collection):
$page_id = (int)$segments[count($segments)-1]; //shows 154
Edit: Here are the outputs of dd($this->crud->query->toSql());
:
//limit access to user owned models
$this->crud->addClause('where','user_id',backpack_user()->id);
//produces "select * from `articles` where `user_id` = ?"
dd($this->crud->query->toSql());
//limit entries to those with samge page_id as in URL
$page_id = \Route::current()->parameter('page_id');
$this->crud->addClause('where','page_id',$page_id);
//produces "select * from `articles` where `user_id` = ? and `page_id` = ?"
dd($this->crud->query->toSql());
Thanks for any help!