Passing multiple parameters to controller in Laravel 5
Asked Answered
S

6

32

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?

Scribbler answered 28/7, 2015 at 16:8 Comment(2)
Can you show us your route definition?Ac
Sure, I updated the original question.Scribbler
I
80

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]);
Incertitude answered 28/7, 2015 at 16:44 Comment(1)
how can we pass multiple optional parameters and use them?Gossipy
F
5

Route :

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

View :

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

}
Fourchette answered 30/5, 2018 at 3:57 Comment(1)
Dear brother, how is it possible? You declare route as delete-warden and call route as delete-buildingAtelectasis
H
1
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;
}
Hesione answered 30/11, 2020 at 5:55 Comment(0)
T
1

This is how you do it:

<a href="{{route('route.name',['param1'=>$variable1,'param2'=>$variable2])}}">Click Here</a>
Tectonics answered 11/11, 2021 at 18:3 Comment(0)
T
0

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
Trin answered 10/12, 2015 at 17:48 Comment(0)
D
0

routes/web.php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BookController;

Route::controller(BookController::class)->group(function () {
     Route::get('author/{author_name}/book/{title}', 'show')
      ->name('book.show');
 });

Now update the controller like:

app/Http/Controllers/BookController.php

namespace App\Http\Controllers;

use App\Models\Book;
use App\Models\Author;
use Illuminate\Http\Request;

class BookController extends Controller
{

  public function show(Request $request, Author $author, Book $book)
  {   
    return view('show',[
        'book' => $book->show($request)
    ]);
  }
}

Now update the book model:

app\Models\Book.php

namespace App\Models;

use App\Common\HasPdf;
use App\Common\HasImage;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Support\Facades\URL;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Book extends Model
{
  use HasFactory;

  protected $guarded = [];

  public function author() : BelongsTo
  {
     return $this->belongsTo(Author::class);
  }


  public function url()
  {
    return URL::route('book.show', [
        'author_name' => $this->author->author_name,
        'title'   => $this->title,
    ]);
  }
}

<a href="{{ $item->url() }}"><h3>{{ $item->title }}</h3></a>

Hope it can help you.

Desirable answered 11/11, 2022 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.