请帮我解决这个路线错误

时间:2016-09-27 07:41:39

标签: php laravel

路线

Route::get('board/{category}', ['as' => 'board.showByCate', 'uses' =>'BoardController@showByCate']);

控制器

public function cate() {

    $categories = category::all();

    return view('welcome', compact('categories'));

}

public function showByCate($category) {

    $boards = board::where('category', '=', $category)->orderBy('created_at', 'desc')->paginate(3)->get();

    $allBoards = board::orderBy('created_at', 'desc')->paginate(3);

    return view('board.index', compact('boards', 'allBoards'));

}

查看

       <ul class="list-inline">
                    <li>
                        <a href="{{ route('board.showByCate') }}">all</a>
                    </li>
                    @foreach($categories as $category)
                        <li>
                            <a href="{{ route('board.showByCate', $category->category) }}">
                                {{$category->category}}
                            </a>
                        </li>
                    @endforeach
                </ul>

此代码带来错误.. 1/2 UrlGenerationException.php第17行中的UrlGenerationException: 缺少[Route:board.showByCate] [URI:board / {category}]的必需参数。 2/2 UrlGenerationException.php第17行中的ErrorException: 缺少[Route:board.showByCate] [URI:board / {category}]的必需参数。 (查看:/home/vagrant/Code/Laravel/resources/views/welcome.blade.php)

我该如何解决这个错误//

1 个答案:

答案 0 :(得分:2)

您尝试使用route('board.showByCate')构建网址,但路由中包含{category}参数。

因此,您需要使用route()传递此参数:

route('board.showByCate', ['category' => 'someCategory'])

或者你可以make parameter optional

Route::get('board/{category?}', ....