php 将多个参数传递给 Laravel 5 中的控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31681715/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:34:17  来源:igfitidea点击:

Passing multiple parameters to controller in Laravel 5

phplaravellaravel-5blade

提问by Cameron Scott

In my application, a user has the ability to remind another user about an event invitation. To do that, I need to pass both the IDs of the event, and of the user to be invited.

在我的应用程序中,用户可以提醒另一个用户有关活动邀请的信息。为此,我需要传递事件的 ID 和要邀请的用户的 ID。

In my route file, I have:

在我的路线文件中,我有:

Route::get('events/{id}/remind', [
'as' => 'remindHelper', 'uses' => 'EventsController@remindHelper']);

In my view, I have:

在我看来,我有:

{!!link_to_route('remindHelper', 'Remind User', $parameters = array($eventid = $event->id, $userid = $invitee->id) )!!}

In my controller, I have:

在我的控制器中,我有:

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

However, when I hit that route, I receive the following error:

但是,当我到达该路线时,收到以下错误:

Missing argument 2 for App\Http\Controllers\EventsController::remindHelper()

I'm sure I have a formatting error in my view, but I've been unable to diagnose it. Is there a more efficient way to pass multiple arguments to a controller?

我确定我认为我有格式错误,但我一直无法诊断它。有没有更有效的方法将多个参数传递给控制器​​?

回答by Arthur Samarcos

When you define this route:

当您定义此路由时:

Route::get('events/{id}/remind', [
'as' => 'remindHelper', 'uses' => 'EventsController@remindHelper']);

You are saying that a single URI argument will be passed to the method.

您是说将单个 URI 参数传递给该方法。

Try passing the two arguments, like:

尝试传递两个参数,例如:

Route::get('events/{event}/remind/{user}', [
'as' => 'remindHelper', 'uses' => 'EventsController@remindHelper']);

View:

看法:

route('remindHelper',['event'=>$eventId,'user'=>$userId]);

回答by Sanaulla

Route:

路线:

Route::get('warden/building/{buildingId}/employee/{employeeId}',[
    'uses'=>'WardenController@deleteWarden',
    'as'=>'delete-warden'
]);

View:

查看

<a href="{{route('delete-building',[$building->id])}}"></a>

Controller:

控制器

public function deleteWarden($buildingId,$employeeId){
    $building = Building::find($buildingId);
    $building->employees()->detach($employeeId);
    return redirect('warden/assign/'.$buildingId)->with('message','Warden Detached successfully');

}

回答by Sanjid Mishu

Go to your controller and write code like following:

转到您的控制器并编写如下代码:

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