Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mani4545's avatar

Route with multiple Id's (Laravel)

I want a URI like this

http://localhost:8000/category/1/3

The first id is Category_id and second is Food_id.

My route is:

Route::get('category/{Category_id?}/{Food_id?}', 'DetailsController@categ');

And in Controller I have:

public function categ($Category_id,$Food_id)
 {
         $category = Categories::with('food')->findOrFail($Category_id);
        $food = Food::with('restaurant','categories')->findOrFail($Food_id); 
         return view('category', compact('category','food'));
 }

But it gives error Missing argument 2 for App\Http\Controllers\Detailscontroller::categ().Can anyone tell where is the problem.I am new to laravel.What I want to do is first shows food items based on category_id and then shows the deatails of foods according to food_id.

0 likes
2 replies
tomopongrac's avatar

You need set default value if you dont provide values in link

public function categ($Category_id ==null,$Food_id == null)
 {
         $category = Categories::with('food')->findOrFail($Category_id);
        $food = Food::with('restaurant','categories')->findOrFail($Food_id); 
         return view('category', compact('category','food'));
 }
Mani4545's avatar

For showing relevant category of foods,in my view I have

@foreach ($Category as $categories) 
<a  href="category/{{$categories->Category_id}}">{{$categories->CategoryName}} </a>
 @endforeach 

and it shows me food items.Then I want when I click on any food item it shows me detail based on food_id. so my nxt view look like:

@foreach ($category->food as $food)
  <a  href="category/{{$food->Category_id}}/{{$food->Food_id}}">{{ $food->FoodName }}</a>
  </div>
@endforeach

But it does not goes to URl like I want.

Please or to participate in this conversation.