Hello artisan,
In this how to pass two parameters in url in laravel 9 tutorial, I will show you how to pass multiple parameters in route and receive them from controller in Laravel. You know that sometimes we need to pass multiple parameters in laravel routes.
So from this example, you will learn how to pass multiple parameters in route in laravel. In this article, we will implement how to pass two parameters in route in laravel. Laravel Routes provides a convenient way to pass multiple parameters to the URL. I will give you two ways to pass multiple parameters to the route. so, let's see the following examples:
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 book model like that:
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,
]);
}
}
Now write this show method in your 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 create URL from that route in your blade like:
Example 2: Laravel Route Pass Multiple Parameters
Now let's have a look at another example:
routes/web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
Route::get('users/{user_id}/posts/{post_id}',[UserController::class, 'show'])->name("users.posts.show");
Now in your controller:
app/Http/Controllers/UserController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function show(Request $request, $user_id, $post_id)
{
dd($user_id, $post_id);
}
}
Now in your HTML, create a URL like:
Example 3: Pass Multiple Parameters with Route Model Binding
Now see another way with route model binding:
routes/web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('users/{user}/posts/{post}',[UserController::class, 'show'])->name("users.posts.show");
Now look at the controller code like:
app/Http/Controllers/UserController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Post;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function show(Request $request, User $user, Post $post)
{
dd($user->toArray(), $post->toArray());
return response()->json(['success'=>'User Updated Successfully!']);
}
}
Now in your blade:
Hope it can help you.
#laravel #laravel-routing