Query string not working in laravel 5
In the following code, I used {!! URL::route('editCatForm',['id'=>$row->id]) !!}
to navigate to the named route editCatForm with a query string ?id=5
or whatever comes dynamically on$row->id
@foreach($categories as $row)
<tr>
<td>{{ $count++ }}</td>
<td>{{ $row->category_name }}</td>
<td>{{ $row->category_status }}</td>
<td><a href="{!! URL::route('editCatForm',['id'=>$row->id]) !!}">edit</a> / <a href="{!! URL::route('destroyCat',$row->id) !!}">delete</a></td>
</tr>
@endforeach
My route for this
Route::get('editCatForm/{id?}',array('uses'=>'Categories@editCat','as'=>'editCatForm'));
but still it shows url as
http://localhost/projects/brainlaratest/editCatForm/2
instead
http://localhost/projects/brainlaratest/editCatForm?id=2
Route points to function
public function editCat($id)
{
$catEdit = Category::find(Input::get('id'));
$categories = $this->getCat();
return view('categoriesAddForm',compact('categories','catEdit'));
}
What could be the problem that the query string is not working here?
source to share
The format of your url editCatForm/{id?}
, so if you provided an id it will try to replace {id}
with your number and you will get editCatForm/5
.
The problem is in your controller action. function editCat($id)
already takes $id
off the route - you Input::get('id')
only have to replace $id
.
URL::route(...)
can only be replaced by a helper function route(...)
.
If you want to get rid of /id
, you can remove {id}
from your route and then route(...)
just add ?id=5
instead /5
. You will need to remove the argument $id
from the function and get the ID Request::input('id');
.
source to share
How route () function works.
If you insist on having a query string, then you need to manually add the url to the url and you won't be able to route based on that.
One way to construct the query string is like this
$params = array('id' => 5);
$queryString = http_build_query($params);
URL::to('projects/brainlaratest/editCatForm?'.$queryString )
source to share
The routing is correct. Your problem is getting $id
in action.
Since you passed $id
in the route parameter, you can capture the segment $id
inside your action.
public function editCat($id)
{
$catEdit = Category::find($id); // edit this line.
$categories = $this->getCat();
return view('categoriesAddForm',compact('categories','catEdit'));
}
source: http://laravel.com/docs/5.0/routing#route-parameters
source to share