Passing multiple parameters to controller in Laravel 5

时间:2015-07-28 16:08:45

标签: php laravel laravel-5 blade

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

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?

4 个答案:

答案 0 :(得分:39)

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.

Try passing the two arguments, like:

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

View:

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

答案 1 :(得分:2)

路线

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

查看

<a href="{{route('delete-building',[$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');

}

答案 2 :(得分:0)

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

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 :(得分:0)

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