请帮助我解决此路线错误

静明

路线

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>

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

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

阿列克谢·梅泽宁

您正在尝试使用构建网址route('board.showByCate'),但您的路由具有{category}参数。

因此,您需要将此参数传递给route()

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

或者,您可以将参数设为可选

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章