Okipa / laravel-table

Generate tables from Eloquent models.
MIT License
532 stars 64 forks source link

route parameters problem #30

Closed afkhami7e closed 4 years ago

afkhami7e commented 4 years ago

Hello dear. i think laravel-table has a fundamental problem it's in route. when use a route to params, replaced 'rows','sort_by','sort_dir' instead my params for exmaple i defined this route: Route::name('store.products.add.index')->get('store/{storeId}/products/{orderStoreId}/aqda', 'ProductStoreController@index'); and defined index route in my controller: $table = (new Table())->request($request)->model(ProductStore::class)-> ...... ->routes([ 'index' => ['name' => 'store.products.add.index', 'params' => [7, 2]] ]) when click on sort button i have this url: https://site/store/20/products//aqda?sort_by=regular_price&sort_dir=desc&7&2 7 & 2 are my route parameters but they are end of url.

I trace and check your code; i found its problem. in the Table.php file, line 282 replace this code: array_merge(Arr::get($this->routes[$routeKey], 'params', []),$params) instead this: array_merge($params, Arr::get($this->routes[$routeKey], 'params', []))

thanks for you, Mahdiyar

afkhami7e commented 4 years ago

Hi, same this problem exist in searchable...

Okipa commented 4 years ago

Hi @mehdi7e, thanks for pointing this out ! Would you mind to send a PR for this ? I am currently in a family weekend without my computer. I will look into it as soon as I am back home.

Okipa commented 4 years ago

Hi @mehdi7e,

I looked into your PR and the issue you've been creating.

There is currently no issue there, since Laravel 6, all parameters given without key will be added to the route as GET params.
Moreover, it is recommended to always pass a key with a route param value, in order to say to Laravel which part of the URL should be replaced. The only way to avoid the route keys is to keep the exact same order between declaration and URL generation.

You should look into this to check how to declare your routes properly with this package : https://github.com/Okipa/laravel-table#-routes
=> The route declaration has now to be done carefully, according to the type of declaration : explicit or implicit.

You can retrieve the tests done to check the routes behaviour from this line : https://github.com/Okipa/laravel-table/blob/master/tests/Unit/RoutesTest.php#L230
It could help you to understand why the routes are handled this way.