Laravel 5 从 URL 获取 ID

2022-08-30 18:41:53

我有一个表格视图,我可以在其中单击按钮图标并重定向到另一个带有已单击行ID的页面。

@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 

我想要的是从URL中获取id并将其放在变量中,以便我可以在其他页面中使用它。

我目前坚持使用这个控制器功能。

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
    ]);
}

答案 1

这已经很晚了。但为了像我这样的人的利益;

如果您不必在上面的答案所示的方法中执行此操作,则从Laravel 5.0开始(不确定以前的版本),您可以执行

$request->route('id');

这将返回路由上参数的值。id


答案 2

或者只是在Blade中使用它:{{ request()->route('id') }}


推荐