【问题标题】:404 not found when storing data laravel 8存储数据时找不到404 laravel 8
【发布时间】:2021-09-28 16:36:00
【问题描述】:

当我点击提交按钮时,它会返回带有 404 页面的路由

这是刀片

<form method="POST" action="{{ route('comment.store') }}" >
      @csrf
     <input type="text">
     <button type="submit">Add comment</button>
</form>

评论控制器:

    class CommentController extends Controller
{
    public function __construct() {
        $this->middleware('auth');
    }


    public function store(Request $request)
    {
        $film = Film::findOrFail($request->film_id);

        Comment::create([
            'comment' => $request->comment,
            'user_id' => Auth::id(),
            'post_id' => $film->id
        ]);
        return redirect()->route('showFilm', $film->id);
    }
}

这是我的 web.php:

Route::post('comment/store', [CommentController::class, 'store'])->name('comment.store');

【问题讨论】:

  • 您在保存数据后重定向到名为showFilm 的路由。 showFilm 路由是否存在?
  • 看起来你没有任何名为showFilm的路由
  • 我有showFilm,评论表格在showFilm Blade

标签: laravel laravel-blade laravel-8


【解决方案1】:

发生这种情况是因为您使用了findOrFail() 方法,顾名思义,如果找到模型,它将返回您的模型,否则,您将显示一个 404 页面。您应该在 store 方法中验证 film_id

编辑:在您的视图中添加一个带有您的 film_id 的隐藏输入

           <form method="POST" action="{{ route('comment.store') }}" >
            @csrf
            <input type="hidden" name="film_id" value="{{ $film->id }}">
            <div class="form">
                <input type="text" placeholder="Add comment..">
                <button type="submit" class="submit">Add comment</button>
               </div>
           </form>

这样,film_id 将被发送到您的控制器

【讨论】:

  • 还是同样的问题
  • 在你看来,你有没有id }}>?
  • 当我做 dd($film) 它返回 null
  • 不,我没有这个观点
  • 查看我编辑的答案,希望对您有所帮助
【解决方案2】:

您的findOrFail() 方法返回此404 错误,导致$request-&gt;film_id 为空

【讨论】:

  • 如何解决?
猜你喜欢
  • 2021-07-29
  • 1970-01-01
  • 2021-08-06
  • 2021-10-14
  • 2021-06-18
  • 1970-01-01
  • 2021-01-27
  • 2021-08-29
  • 2021-11-06
相关资源
最近更新 更多