Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 - link_to_route() method making my route parameters to change into Query String by adding "?" at the end

After hours of searching I still could not find my answer regarding L5.

What my issue is :

I want to make a link something like this:

localhost:800/songs/you-drive-me-crazy

BUT what is get is:

localhost:800/songs?you-drive-me-crazy

my route parameter is changing into query string.

//routes.php

$router->bind('songs', function($slug)
{
return App\Song::where('slug', $slug)->first();
});


$router->get('songs', ['as' => 'songs.index', 'uses' =>    'SongsController@index'] );

$router->get('songs/{songs}', ['as' => 'songs.show', 'uses' => 'SongsController@show'] );

I am using:

{!! link_to_route('songs.index', $song->title, [$song->slug])  !!}

I have tried everything but not succeeded yet,your suggestion may be helpful.

Thanks.

like image 465
Saj Avatar asked Jun 02 '15 04:06

Saj


People also ask

How can pass query string in URL in Laravel?

You can pass query string to URL in laravel using named route and controller action. You can pass query string as comma separated array to named route and controller action and redirect to URL.

Which method allows you to verify that the incoming request path matches a given pattern?

rs. Path annotation in JAX-RS is used to define a URI matching pattern for incoming HTTP requests. It can be placed upon a class or on one or more Java methods.

What is Route parameter in Laravel?

Laravel routes are located in the app/Http/routes.A parameter provided in the route is usually annotated with curly braces. For instance, to pass in a name parameter to a route, it would look like this. By convention, the Controller function accepts parameters based on the parameters provided.

How do I get params in Laravel?

To retrieve the query parameters on your Laravel backend, you can make use of either the "Request" class or the "request()" helper method. Imagine you want to get the "search" query from the URL, you can do as follows. $searchQuery = $request->query('search');


1 Answers

Your usage of link_to_route is incorrect:

{!! link_to_route('songs.index', [$song->title, $song->slug])  !!}

The first parameter is the route name, the second parameter is an array of route parameters, preferably using key value. Because you did not show your defined route, it's hard to guess what this associative array should look like:

{!! link_to_route('songs.index', ['title'=>$song->title, 'slug'=>$song->slug])  !!}

Also I advise you to use the documented functions: route(), see: http://laravel.com/docs/5.0/helpers#urls

A correctly requested route using route():

{!! route('songs.index', ['title'=>$song->title, 'slug'=>$song->slug])  !!}

A properly formatted route would then be:

Route::get('songs/{title}/{slug}', ['as' => 'songs.index', 'uses' => 'SomeController@index']);

This will result in a URL like: http://localhost:800/songs/you-drive-me-crazy/slug

If you only want to add the title to the URL but not the slug, use a route like this:

Route::get('songs/{title}', ['as' => 'songs.index', 'uses' => 'SomeController@index']);

This will result in a URL like: http://localhost:800/songs/you-drive-me-crazy/?slug=slug

Using

Route::get('songs/{slug}', ['as' => 'songs.index', 'uses' => 'SomeController@index']);

The URL will be like: http://localhost:800/songs/you-drive-me-crazy/?title=title assuming the slug now is you-drive-me-crazy

Any added parameter in a route() call will be added as a GET parameter if it's not existing in the route definition.

like image 66
Luceos Avatar answered Oct 29 '22 13:10

Luceos