Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Variables Passed In URL Laravel

Probably a basic question but I can't seem to get it.

I am wanting to grab the variable in my url to my controller.

// index view

@foreach ($paymentInfos as $p)
        <tr>
             <td><a href="{{ URL::action('AdminController@getPackingSlip', array('order_id' => $p->order_id)) }}"> {{ $p->order_id }}</a></td>
             <td>{{ $p->lastname }} {{ $p->firstname }}</td>
             <td>{{ $p->buyer_email }}</td>
        </tr> 
      @endforeach

// route

Route::get('printpackingslip', 'AdminController@getPackingSlip');

// Controller

class AdminController extends BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function getPackingSlip()
    {

        $rules = array('order_id' => 'order_id');


        return View::make('admin.packingslip')->with($rules);
    }
}

When you click on the link it goes to www.domain.com/printpackingslip?order_id=3

I do not know how to grab the order_id=3 in my controller.

Also would I be better off using the :(num) to generate a URI of /printpackingslip/3 or does it not matter?

For example:

// in my first view have:

<td><a href="{{ URL::to('printpackingslip', array('order_id' => $p->order_id)) }}"> {{ $p->order_id }}</a></td>

// then my route:

Route::get('printpackingslip/(:num)', 'AdminController@getPackingSlip');

Thanks!

like image 205
Nick8675 Avatar asked Dec 26 '22 20:12

Nick8675


1 Answers

Is this Laravel 4? Let's review it all:

Route:

Route::get('printpackingslip/{order_id}', 'AdminController@getPackingSlip');

Controller:

class AdminController extends BaseController {

    public function getPackingSlip($order_id)
    {
        return "you selected $order_id";
    }
}

View:

@foreach ($paymentInfos as $p)
   ...
   <td><a href="{{ URL::route('printpackingslip', array('order_id' => $p->order_id)) }}"> {{ $p->order_id }}</a></td>
   ...
@endforeach
like image 125
Antonio Carlos Ribeiro Avatar answered Jan 05 '23 04:01

Antonio Carlos Ribeiro