【问题标题】:Laravel 5.8 Routed page not found 404 errorLaravel 5.8 找不到路由页面 404 错误
【发布时间】:2019-03-16 05:23:30
【问题描述】:

我是 Laravel 的新手。我正在从教程中学习 Laravel,但遇到了一个我无法解决的问题。
我认为我在路由的某个地方有问题,但我找不到它
有趣的是,如果href{{route('tag.create'}},那么它会去创建页面,但是当我需要使用 ID 时它不起作用...... 我对帖子和类别有相同的功能,但对这两者来说一切都很好。所以我真的需要你的帮助才能看到我看不到的东西。我有这些文件:

index.blade.php:

@extends('layouts.app')

@section('content')

    <div class="card">
        <div class="card-body">
            <table class="table table-hover">
                <thead>
                    <th>
                        Tag name
                    </th>

                    <th>
                        Delete
                    </th>
                </thead>
                <tbody>
                    @if($tags->count()>0)
                        @foreach($tags as $tag)
                            <tr>
                                <td>
                                    {{$tag->tag}}
                                </td>
                                <td>
                                    <a href="{{route('tag.delete', ['id' =>$tag->id])}}" class="btn btn-danger btn-xs"><i class="fa fa-trash" aria-hidden="true"></i></a>
                                </td>
                            </tr>
                        @endforeach
                    @else
                        <tr>
                            <th colspan="5" class="text-center">
                                No tags yet
                            </th>
                        </tr>
                    @endif
                </tbody>
            </table>    

        </div>
    </div>


@stop

web.php - 这是我为TagsController.php 定义标签路由的地方:

//Tags
    Route::get('/tags',[
        'uses'=>'TagsController@index',
        'as'=> 'tags'
    ]);

    Route::post('/tag/update/{$id}',[
        'uses'=>'TagsController@update',
        'as'=> 'tag.update'
    ]);

    Route::get('/tag/create',[
        'uses'=>'TagsController@create',
        'as'=> 'tag.create'
    ]);
    Route::post('/tag/store',[
        'uses'=>'TagsController@store',
        'as'=> 'tag.store'
    ]);
    Route::get('/tag/delete/{$id}',[
        'uses'=>'TagsController@destroy',
        'as'=> 'tag.delete'
    ]);

TagsController.php - 起初我试图破坏元素,然后我试图返回创建视图(因为当我通过 /tag/create rout 时一切正常),但在这里都没有工作

public function destroy($id)
    {
        return view ('admin.tags.create');
        /*
        Tag::destroy($id);

        Session::flash('success', 'Tag deleted succesfully');

        return redirect()->back();*/
    }

【问题讨论】:

  • {$id} 更改为{id}

标签: php laravel


【解决方案1】:

我认为您应该将路由设置为Route::get('/tag/delete/{id}',[ 'uses'=&gt;'TagsController@destroy', 'as'=&gt; 'tag.delete' ]);,因为在您的情况下,您告诉路由期待一个名为$id的变量

【讨论】:

  • 谢谢伙计。恨自己在ID之前没有看到$。
  • 随时伴侣。不要为此而自责,这些事情经常发生。我们忘记并添加无用的$。确保您也在更新中更改它。祝你好运,如果你愿意,请接受我的回答
【解决方案2】:

请将 web.php 中路由设置中的参数从 $id 更改为 id。我应该解决你的问题。

例如:Route::get('/tag/delete/{id}',[ 'uses'=>'TagsController@destroy', 'as'=> 'tag.delete' ]);

谢谢!!。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-20
    • 2017-06-17
    • 2015-02-04
    • 2013-10-07
    • 2020-03-26
    • 2015-01-02
    • 2021-06-19
    • 2013-08-12
    相关资源
    最近更新 更多