RichardMiracles
RichardMiracles

Reputation: 2152

Add query param to url in the Laravel Controller

Is it possible to add a parameter to the url from the route Controller?

For example, my URL is: myshop.com/order-finished

And his method controller is:

getOrderFinished() {
  // I want add this param
  myshop.com/order-finished?order_number=W00034335
}

I want the final URL to be: myshop.com/order-finished?order_number=W00034335, but I must add the parameter order_number from the controller method.

EDIT: no redirections, please.

Upvotes: 2

Views: 14655

Answers (7)

X 47 48 - IR
X 47 48 - IR

Reputation: 1478

Use the request illuminate class.

$url = 'http://localhost';
$qs = [
   'par1' => 'val1',
   'par2' => 'val2',
];

$url = \Illuminate\Http\Request::create($url)->fullUrlWithQuery($qs);

The above code will return http://localhost?par1=val1&par2=val2.

Upvotes: 1

parker_codes
parker_codes

Reputation: 3397

Since all (or most) of the answers are using redirects, I'll try something else. I feel like this is a very clean approach.

In your routes/web.php file, insert this:

Route::get('order/{id}', 'OrderController@show');

And then in your OrderController, have this:

public function show($id)
    {
        $order = Order::find($id);

        return view('orders.show')->with('order', $order);
    }

It's the basic idea of routing, as you can find in the documentation here.

Modify the naming conventions how you will!

Upvotes: 1

omitobi
omitobi

Reputation: 7334

Is it possible to add a param to the url from the route Controller?

Yes, it is possible. You can redirect route if you want to go to that url. Say the route name is 'order-finished' then in the controller you can do the following :

return redirect()->route('order-finished', ['order_number'=>'W00034335']);

The implication is that the order_number will be appended as a query parameter to that route.

PS: This answer goes specifically if you are referring to a request call to a route, and not the general url configuration of your app.

Upvotes: 1

krishna
krishna

Reputation: 98

To add a parameter to the url you need to define in route file

<?php 
Route::get('/order-finished/order_number/{id}', [
                'as' => 'front.order.detail',
                'uses' => 'OrderController@getOrderDetail'
            ]);
{{ route('front.order.detail',$order['id']) }}
?>

Upvotes: 0

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

You should try this:

use Illuminate\Support\Facades\Redirect;


$paramData = 'W00034335';
return Redirect::to('/order-finished?order_number='. $paramData);

Hope this help for you !!!

Upvotes: 1

Sagar Gautam
Sagar Gautam

Reputation: 9359

Simply, use redirect()->to() method.

public function getOrderFinished(){

    // your parameter value

    $param = W00034335;

    return redirect()->to('myshop.com/order-finished?order_number='.$param);
}

Upvotes: 1

Bas
Bas

Reputation: 2400

Yes:

redirect('order-finished?order_number=W00034335');

Upvotes: 0

Related Questions