简体   繁体   English

具有多个Id的路线(Laravel 5.2)

[英]Route with multiple Id's (Laravel 5.2)

I want a URI like this 我想要一个像这样的URI

http://localhost:8000/category/1/3 HTTP://本地主机:8000 /类别/ 1/3

The first id is Category_id and second is Food_id. 第一个id是Category_id,第二个是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. 但是它为App \\ Http \\ Controllers \\ Detailscontroller :: categ()提供了缺少参数2的错误。任何人都可以告诉问题在哪里。我是laravel的新手。我想要做的是先显示基于category_id的食物项目然后根据food_id显示食物的deatails。

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. 它显示了我的食物。然后我想点击任何食物项目时它会根据food_id显示我的细节。 so my nxt view look like: 所以我的nxt视图看起来像:

 @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 . Anish留下的评论是正确的,但是,当您尝试使用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 但是,我会采用更加RESTful的方法: 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: 然后一个控制器为你Foods只有一个show()方法:

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 如果您最终走下RESTful路线(推荐),那么您可能需要查看: https//laravel.com/docs/5.2/routing#route-model-binding

Hope this help! 希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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