在 Laravel 5 中将多个参数传递给控制器
Posted
技术标签:
【中文标题】在 Laravel 5 中将多个参数传递给控制器【英文标题】:Passing multiple parameters to controller in Laravel 5 【发布时间】:2015-10-19 07:52:34 【问题描述】:在我的应用程序中,用户可以提醒其他用户有关活动邀请。为此,我需要同时传递事件的 ID 和被邀请用户的 ID。
在我的路线文件中,我有:
Route::get('events/id/remind', [
'as' => 'remindHelper', 'uses' => 'EventsController@remindHelper']);
在我看来,我有:
!!link_to_route('remindHelper', 'Remind User', $parameters = array($eventid = $event->id, $userid = $invitee->id) )!!
在我的控制器中,我有:
public function remindHelper($eventid, $userid)
$event = Events::findOrFail($eventid);
$user = User::findOrFail($userid);
$invitees = $this->user->friendsOfMine;
$invited = $event->helpers;
$groups = $this->user->groupOwner()->get();
return view('events.invite_groups', compact('event', 'invitees', 'invited', 'groups'));
但是,当我到达该路线时,我收到以下错误:
Missing argument 2 for App\Http\Controllers\EventsController::remindHelper()
我确定我的视图中有格式错误,但我无法诊断它。有没有更有效的方法将多个参数传递给控制器?
【问题讨论】:
你能告诉我们你的路线定义吗? 好的,我更新了原来的问题。 【参考方案1】:当你定义这条路线时:
Route::get('events/id/remind', [
'as' => 'remindHelper', 'uses' => 'EventsController@remindHelper']);
您是说将单个 URI 参数传递给方法。
尝试传递两个参数,例如:
Route::get('events/event/remind/user', [
'as' => 'remindHelper', 'uses' => 'EventsController@remindHelper']);
查看:
route('remindHelper',['event'=>$eventId,'user'=>$userId]);
【讨论】:
我们如何传递多个可选参数并使用它们?【参考方案2】:转到您的控制器并编写如下代码:
public function passData()
$comboCoder=['Bappy','Sanjid','Rana','Tuhin'];
$ffi=['Faisal','Sanjid','Babul','Quiyum','Tusar','Fahim'];
$classRoom=['Sanjid','Tamanna','Liza'];
return view('hyper.passData',compact('comboCoder','ffi','classRoom'));
/*
Again, in View part use:
(passData.blade.php)
*/
<u>Combocoder:</u>
@foreach($comboCoder as $c)
$c<br>
@endforeach
<u>FFI</u>
@foreach($ffi as $f)
$f<br>
@endforeach
<u>Class Room </u>
@foreach($classRoom as $cr)
$cr<br>
@endforeach
【讨论】:
【参考方案3】:路线:
Route::get('warden/building/buildingId/employee/employeeId',[
'uses'=>'WardenController@deleteWarden',
'as'=>'delete-warden'
]);
查看:
<a href="route('delete-warden',[$building->id])"></a>
控制器:
public function deleteWarden($buildingId,$employeeId)
$building = Building::find($buildingId);
$building->employees()->detach($employeeId);
return redirect('warden/assign/'.$buildingId)->with('message','Warden Detached successfully');
【讨论】:
亲,怎么可能?您将路由声明为delete-warden
,并将路由调用为delete-building
【参考方案4】:
Route::get('/details/id/id1/id2', 'HomeController@SearchDetails');
//pass data like the below code
<a href="url("/details/$orga_list->dcode/$orga_list->dname/$GroupHead")"
target="_blank" > Details </a>
//controller write like the below code
public function SearchDetails($id, $searchtext,$grp_searchtext)
// get data like the below code
$data['searchtext'] = $searchtext;
$data['grp_searchtext'] = $grp_searchtext;
$data['id_is'] = $id;
【讨论】:
【参考方案5】:这就是你的做法:
<a href="route('route.name',['param1'=>$variable1,'param2'=>$variable2])">Click Here</a>
【讨论】:
以上是关于在 Laravel 5 中将多个参数传递给控制器的主要内容,如果未能解决你的问题,请参考以下文章
如何在 PySpark 中将多个参数传递给 Pandas UDF?
如何在 Laravel 8.x 中将附加参数传递给验证器-> 后函数?