-
Notifications
You must be signed in to change notification settings - Fork 902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature Request] link() helper on CrudColumn #5317
Conversation
[ci skip] [skip ci]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @karandatwani92 ,
I could NOT get this to work 😔
Try 1. In our demo, tried in OwnerCrudController, throws an array to string conversion
error.
CRUD::column('owner')->linkTo('owner.show');
Try 2. In our demo, tried in ArticleCrudController, tried this but all links point to the same entry, with the ID of 1:
$this->crud->addColumn([
'label' => 'Category',
'type' => 'select',
'name' => 'category_id',
'entity' => 'category',
'attribute' => 'name',
]);
$this->crud->column('category_id')->linkTo('category.show');
Try 3. In our demo, tried in CategoryCrudController to do the below, but I get the same array to string conversion
error:
CRUD::column('parent')->linkTo('category.show');
Try 4. In our demo, tried in CategoryCrudController to do the below and I get the same array to string conversion
error:
CRUD::column('articles')->linkTo('article.show');
So... no matter what I try, this doesn't work as expected for me. What's happening here? Am I using this wrong? AFAIK, I'm using it exactly like in the PR instructions.
Let me re-define my expectations here:
- I expect to be able to do
->linkTo('entity.show')
on any column... and for it to add a link to the show operation, towards that particular connected entry; click to go see that related entry; - I expect to be able to use this
linkTo()
helper on any Backpack column type that has a relationship, in CRUD or PRO; - I expect to be able to use this
linkTo()
helper on both single-relationship columns, and multiple-relationship columns;
What's gone wrong here? Are my expectations too high? Does the PR only treat one particular use case, that I haven't tested?
You want to try a re-do on this PR?
Cheers!
Decided to postpone this to next month, in order for @karandatwani92 to be able to finish this. @karandatwani92 when you're back, please give my review above a look, fix things and talk to Antonio & Pedro. |
@pxpm please take over and let's get this past the finish line this month 💪 |
[ci skip] [skip ci]
[ci skip] [skip ci]
@tabacitu docs can be found here: Laravel-Backpack/docs#519 Please do the final review and let me know if we have the 🚥 here. Cheers |
Hi guys, I like this new feature, but, how do we handle links such as
|
Hi @tabacitu, In my code i have an article crud and a category crud.
When i use it on a
I'll stick with my implementation, keep it simple and let laravel throw errors if the route is not configured properly.
Configuration:
|
Hey @susanu thanks for the suggestions. I agree with the use case you presented, re-activating filters on list pages, but there are others that we are consciously leaving behind, like 1 - I want to redirect a user to a List Page with some filter pre-activated that does not depend on $entry: CRUD::column('article')->linkTo('articles.list', ['filterName' => 'filterValue']);
// VS
CRUD::column('article')->wrapper(['href' => backpack_url('articles?filterName=filterValue')]); 2 - I want to redirect a user to a List Page with some filters pre-activated that depend on $entry: CRUD::column('article')->linkTo('articles.list', [
'filterName' => function($crud, $column, $entry, $related_key) {
return $entry->someAttribute;
},
'otherFilter' => function($crud, $column, $entry, $related_key) {
return $entry->otherAttribute;
},
]);
// VS
CRUD::column('article')->wrapper(['href', function($crud, $column, $entry, $related_key) {
return backpack_url('articles?filterName='.$entry->someAttribute.'&otherName='.$entry->otherAttribute); // or a sprintf or whatever your flavor.
}]);
//So what about this:
CRUD::column('article')
->linkTo('articles.list', [
LinkParameter::entry('filterName', fn($entry) => $entry->someAttribute),
LinkParameter::related('id'), // we know it's to use the `$related_key` in a param named `id`
LinkParameter::for('otherName', fn($crud, $column, $entry, $related_key) => $column['smt'] ? $entry->bla : $entry->ble),
])
); I don't see the point for the linkTo() in this advanced usecases if we don't completely over-haul the implementation to have a "nicer" DX. So to answer @tabacitu my responses to both questions are I think @susanu may have some
There is a reason for that to be there, otherwise not all the columns will work as you can see from what the code is doing. I will test the columns that you guys said are failing as that's independent from the DX. Cheers |
Hi @pxpm, Regarding the error Try using this implementation:
This covers most of the use cases, i think:
Also, i love this
|
I like the how Hmmm I like this too, seems like a simple solution to a complex problem: CRUD::column('article')->linkTo(route('articles.list', ['filterName' => 'filterValue']));
CRUD::column('article')->linkTo(backpack_url('articles?filterName=filterValue'));
CRUD::column('article')->linkTo(fn($entry, $related_key) => route('articles.list', ['filterName' => $entry->getKey()]));
CRUD::column('article')->linkTo(fn($entry, $related_key) => 'https://external-url.com/article/preview/' . $entry->getKey());
CRUD::column('article')->linkTo('https://google.com/', '_blank');
CRUD::column('article')->linkTo('articles.list'); But what I don't see is how it would cover the link to the Show operation, where we would need to inject the ID of the current entry. Would that be covered by So what does the parameter for this function become? Docs:
The only thing I don't like here is that |
Yes My humble opinion is that we can live with the first parameter being a wildcard instead of having 2 options for defining a url ( |
The suggested code by @susanu wouldn't work, but the idea might work yes. Route::get('article/{article}/publish')->name('article.publish');
// http://demo.test/admin/article/1/publish
Route::get('article/{id}/show')->name('article.show')
// http://demo.test/admin/article/1/show
Route::get('article')->name('article.list')
// http://demo.test/admin/article If you defined: But what if you want to add an additional url parameter when publishing/showing an article eg: CRUD::column('article')
->linkTo('article.publish', [
'article' => fn($entry, $related_key) => $related_key,
'mode' => fn($entry) =>$entry->someAttribute`
]);
// is the same as:
CRUD::column('article')
->linkTo('article.publish', [ 'mode' => fn($entry) =>$entry->someAttribute`]);
// the $related_key parameter (article) will be automatically added since it's missing in the parameter list To sum up the rules:
Are we on-board with this ? |
Yes, looks good to me. |
@tabacitu did the changes we discussed here. Added tests for all the scenarios I could remember, do you mind testing the scenarios you found failing or see if I miss them in the tests ? @susanu anything you think I didn't covered or you would do differently ? Updated docs are here: Laravel-Backpack/docs#519 Cheers |
Woohooo! Excellent work here @pxpm . I think you really knocked it out of the park! I tested this in multiple scenarios and found no problems. It works exactly as I expect it to. Excellent breakdown in the comment above too 👏👏👏 |
[ci skip] [skip ci]
…omments Columns link to with comments
WHY
Solves Laravel-Backpack/community-forum#8
Context
How to use