简体   繁体   中英

Route with multiple Id's (Laravel 5.2)

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.

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>
 @endforeach

The comment Anish left was correct, however, you main problem will come when you're trying to find models with null . To get around this you could have something like:

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

NB They may be more errors in your view file depending on if you're trying to access.

However, I would go with a much more RESTful approach: https://laravel.com/docs/5.2/controllers#restful-resource-controllers

Essentially, this would mean having a controller for you Categories:

public function index() {
    //Code to get all categories (if you have a lot you may want to paginate them)
}

public function show($Category_Id) {
    $category = Categories::with('food')->findOrFail($Category_id);

    //etc
}

and then a controller for you Foods with just a show() method:

public function show($Food_Id) {
    $food = Food::with('restaurant','categories')->findOrFail($Food_id);
}

OR depending on how you set your route up you could also include the category as well if you need to (but if it's just a one2Many relationship it might be redundant) so you would have

public function show($category_ID, $Food_Id) //etc

Your routes would then be set up like this:

Route::get('categories', 'CategoriesController@index');
Route::get('categories/{$category_id}', 'CategoriesController@show');

//Assuming you go with the first option - something like:
Route::get('foods/{$food_id}', 'FoodsController@show');

//Assuming you go with the section option for Foods
Route::get('categories/{$category_id}/{$food_id}', 'FoodsController@show');

Obviously, the above is just an example so feel free to set you controllers/routes up how you like.

If you do end up going down the RESTful route (recommended) you then might want to look at: https://laravel.com/docs/5.2/routing#route-model-binding

Hope this help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM