Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 getting ID from URL

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
    ]);
}
like image 925
Kent Abrio Avatar asked Oct 21 '15 18:10

Kent Abrio


People also ask

How do I find URL ID?

To Get the ID from URL with JavaScript, we can call the JavaScript string's split method on the URL string. Then we get the last part of it with the JavaScript array at method. We call split with '/' to split the url string by the / . Then we call at with -1 to get the element from the strs array.

How can I get laravel 8 Controller ID?

$id = Auth::id(); // Retrieve the currently authenticated user's ID... $user = $request->user(); // returns an instance of the authenticated user... $id = $request->user()->id; // Retrieve the currently authenticated user's ID... $user = auth()->user(); // Retrieve the currently authenticated user...


3 Answers

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.

like image 57
Daniel Verem Avatar answered Oct 23 '22 19:10

Daniel Verem


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

like image 27
Harry Bosh Avatar answered Oct 23 '22 19:10

Harry Bosh


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
}
like image 22
Akshendra Pratap Avatar answered Oct 23 '22 21:10

Akshendra Pratap