26

I have a table view in which I can click an button icon and redirect to another page carrying the id of the row that has been clicked.

@foreach ($patients as $patient)
    <tr>
        <td>{{ $patient->pID }}</td>
        <td>{{ $patient->pName }}</td> 
        <td>{{ $patient->pAddress }}</td>
        <td>{{ $patient->pBday }}</td>
        <td>{{ $patient->pPhone}}</td>
        <td>{{ $patient->pEcon }}</td>
        <td>{{ $patient->pDreg }}</td>
        <td></td>
        <td>
            <a href="{{ URL::to('visit/'.$patient->pID) }}">
                <img src="../images/viewvisit.png" style="width:30px; height:30px;">
            </a>
        </td>
        <td>
            <a href="{{ URL::to('addeditvisit/'.$patient->pID) }}">
                <img src="../images/visit.png" style="width:30px; height:30px;">
            </a>
        </td>
        <td>
            <a href="{{ URL::to('editpatient/'.$patient->pID) }}">
                <img src="../images/update.png" style="width:30px; height:30px;">
            </a>
        </td>
        <td>
            <a href="{{ URL::to('deletepatient/'.$patient->pID) }}">
                <img src="../images/delete.png" style="width:30px; height:30px;">
            </a>
        </td>
    </tr>
@endforeach 

what I want is to get the id from the URL and put it in a variable so that I can utilize it my other page.

I'm currently stuck with this controller function.

public function index(Request $request) {   

    $doctors = Doctor::where('dStatus', 0)
        ->lists('dName', 'dID');
    $beds = Bed::where('bStatus', 0)
        ->lists('bName', 'bID');
    $patient = Patient::patient();
    // $uri = $request->path('patient');

    return View::make('pages.addeditvisit', [
        'doctors'=>$doctors,
        'beds'=>$beds,
        'patient'=>$patient->pID
    ]);
}
nyedidikeke
  • 6,899
  • 7
  • 44
  • 59
Kent Abrio
  • 445
  • 2
  • 9
  • 27

7 Answers7

42

This is late. But for the benefit of others like me;

If you do not have to do it in a method like the answers above have shown, As of Laravel 5.0 (Not sure about previous versions), you can do

$request->route('id');

That returns the value of the id parameter on the route.

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
Verem Dugeri
  • 630
  • 9
  • 15
32

Or just use it within Blade: {{ request()->route('id') }}

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
Harry Bosh
  • 3,611
  • 2
  • 36
  • 34
23

Basically when you are defining the routes, you use something called route parameters, something like this

Route::get('/visit/{id}', 'Controller@someMethod');

This id will be available as a parameter in your handler funtion,

public function someMethod($id) {
    // you have the id here
}
Akshendra Pratap
  • 2,002
  • 1
  • 12
  • 25
4

Simple example:

as link=>   example.com/user/1

as rout=> Route::get('user/{id}', 'UserController@user');

as UserController function 

public function user($id){

    echo $id;

}

output => 1
tuomastik
  • 4,559
  • 5
  • 36
  • 48
Bhale Dino
  • 248
  • 2
  • 7
1

The trick is to declare the url's structure at routes including the id, for example:

// {{ URL::to('editpatient/'.$patient->pID) }}
Route::get('editpatient/{patientId}', 'MyController@index');

Then, just inject the id in the controller's function:

public function index($patientId){
    // $patientId is the variable from url
}
manix
  • 14,537
  • 11
  • 70
  • 107
1

Please refer to the answered question for how to get a parameter from a route. But if you stumbled on this old Laravel thread looking for how to retrieve a model by its ID, the simplest way is to instantiate the model in the controller's request parameter:

Route::get('/retrieve_product/{product}', 'SearchController@getProduct');

Then over in your controller, you simply need:

use App\Product;

class SearchController extends Controller
{
    public function getProduct( Product $product ) {
        return $product;
    }
}

That's it. No find, no where, no get, no first etc.

So, in this example, if you visit /retrieve_product/1 the first product is returned to you.

Grant
  • 5,709
  • 2
  • 38
  • 50
0
Route::get('post/user/{id}','ProductController@allpost')->where('id', '[0-9]+');

Controller

public function allpost($id)
    {
        $products = Product::where('uploadby', $id)->orderBy('created_at','desc')->paginate(5); <br>
        return view('product.user_product')->with('products', $products);
    }
Myat Htut
  • 487
  • 2
  • 11
  • 17