php Laravel 5 从 URL 获取 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33266316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 getting ID from URL
提问by Kent Abrio
I have a table view in which I can click an button icon and redirect to another page carrying the idof the row that has been clicked.
我有一个表格视图,我可以在其中单击按钮图标并重定向到另一个带有已单击行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
what I want is to get the idfrom the URL and put it in a variable so that I can utilize it my other page.
我想要的是从 URL 中获取id并将其放入一个变量中,以便我可以在我的其他页面中使用它。
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
]);
}
回答by Harry Bosh
Or just use it within blade
或者只是在刀片内使用它
{{request()->route('id')}}
{{request()->route('id')}}
回答by Akshendra Pratap
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,
此 ID 将作为处理程序函数中的参数提供,
public function someMethod($id) {
// you have the id here
}
回答by Daniel Verem
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
如果您不必按照上述答案所示的方法执行此操作,则从 Laravel 5.0 开始(不确定以前的版本),您可以执行
$request->route('id');
That returns the value of the id
parameter on the route.
这将返回id
路由上参数的值。
回答by Bhale Dino
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
回答by manix
The trick is to declare the url's structure at routes including the id, for example:
诀窍是在包含 id 的路由中声明 url 的结构,例如:
// {{ URL::to('editpatient/'.$patient->pID) }}
Route::get('editpatient/{patientId}', 'MyController@index');
Then, just inject the id in the controller's function:
然后,只需在控制器的函数中注入 id:
public function index($patientId){
// $patientId is the variable from url
}
回答by Myat Htut
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);
}