沃梦达 / 编程问答 / php问题 / 正文

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

Passing multiple parameters to controller in Laravel 5(将多个参数传递给 Laravel 5 中的控制器)

本文介绍了将多个参数传递给 Laravel 5 中的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

In my route file, I have:

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

在我看来,我有:

{!!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 AppHttpControllersEventsController::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?

推荐答案

当你定义这个路由时:

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

您是说将向该方法传递单个 URI 参数.

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

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

Try passing the two arguments, like:

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

查看:

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

这篇关于将多个参数传递给 Laravel 5 中的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将多个参数传递给 Laravel 5 中的控制器

基础教程推荐