Laravel 5从URL获取ID

27

我有一个表格视图,在其中可以点击一个按钮图标并重定向到另一个页面,携带了已被点击的行的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
    ]);
}

你是如何定义你的路由的? - Akshendra Pratap
7个回答

43

虽然有点晚了,但是为了像我一样的其他人的利益;

如果您不必像上面的答案所示那样以某种方法执行此操作,在Laravel 5.0(之前的版本不确定)中,您可以执行

$request->route('id');

该函数返回路由中 id 参数的值。


32

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


23

基本上在定义路由时,您会使用称为路由参数的东西,类似于这样

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

这个ID将作为参数在您的处理函数中可用,

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

1
如果你不在那个页面上怎么办? - Dustin Graham

4

简单示例:

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

1
关键在于在路由中声明URL的结构,包括ID,例如:
// {{ URL::to('editpatient/'.$patient->pID) }}
Route::get('editpatient/{patientId}', 'MyController@index');

然后,在控制器的函数中注入该ID:
public function index($patientId){
    // $patientId is the variable from url
}

1
请参考已回答的问题,了解如何从路由中获取参数。但是,如果你在寻找如何通过ID检索模型的过程中遇到了这个旧的Laravel线程,最简单的方法是在控制器的请求参数中实例化该模型:
Route::get('/retrieve_product/{product}', 'SearchController@getProduct');

然后在你的控制器中,你只需要:
use App\Product;

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

这就是全部。没有find、where、get、first等等。
因此,在这个例子中,如果你访问/retrieve_product/1,第一个产品会被返回给你。

1
另外,请注意,如果给定ID的数据不存在,它将返回错误404。 - ibnɘꟻ

0
Route::get('post/user/{id}','ProductController@allpost')->where('id', '[0-9]+');

控制器

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接