Laravel 5 gets identifier from URL

I have a table view in which I can click the button icon and redirect to another page that indicated the identifier of the row that was 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 

I want to get the id from the url and put it in a variable so that I can use it on another page.

I am currently sticking to this controller feature.

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
    ]);
}
+4
source share
6 answers

Basically, when you define routes, you use something called route parameters , something like this

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

This identifier will be available as a parameter in your handler,

public function someMethod($id) {
    // you have the id here
}
+10

{{request()->route('id')}}

+5

, url , , :

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

:

public function index($patientId){
    // $patientId is the variable from url
}
+1
source

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

If you don't need to do this in a method similar to the answers above, with Laravel 5.0 (not sure about previous versions) you can do

$request->route('id');

Returns the value of the parameter idon the route.

+1
source
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);
    }
0
source

A 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
0
source

Source: https://habr.com/ru/post/1612628/


All Articles