3

In the following code I used {!! URL::route('editCatForm',['id'=>$row->id]) !!} to go to named route editCatForm with query string ?id=5 or whatever that 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 is

Route::get('editCatForm/{id?}',array('uses'=>'Categories@editCat','as'=>'editCatForm'));

but still it shows url like

http://localhost/projects/brainlaratest/editCatForm/2

instead of

http://localhost/projects/brainlaratest/editCatForm?id=2

The route points to function

public function editCat($id)
{
    $catEdit = Category::find(Input::get('id'));
    $categories = $this->getCat();
    return view('categoriesAddForm',compact('categories','catEdit'));
}

What may be the issue that query string isn't working here?

3 Answers 3

3

Format of your url is editCatForm/{id?} so if you provided id it will try to replace {id} with your number and you will get editCatForm/5.

Problem is in your controller action. function editCat($id) already takes $id from route - you should replace Input::get('id') with just $id.

URL::route(...) can be replaced by just helper function route(...).

If you want get rid of /id you can remove {id} from your route and then route(...) will just add ?id=5 instead of /5. You would have to remove $id argument from function and get id by Request::input('id');.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply,do I need to include Use Request; or something on top of my controller to use request? and I need to make url editCatForm instead of editCatForm/{id?} ,is it?
Yes, you have to put use Request; on the top. If you really do not want to have /id then Route::get('editCatForm' ...) is what you want.
Tons of thanks sir!Your solution really did worked!!
1

This is how route() function is supposed to work.

If you insist on having the query string then you need to append the ?=2 to the URL manually and you cannot do routing based on this.

One way of building the query string is like this

$params = array('id' => 5);
$queryString = http_build_query($params);
URL::to('projects/brainlaratest/editCatForm?'.$queryString )

Comments

0

The Routing is correct. Your problem is to getting the $id in the action.

Since, you have passed $id in 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.