【问题标题】:Laravel: Resource controller 'store' method produces no results, no errorsLaravel:资源控制器“存储”方法不产生任何结果,没有错误
【发布时间】:2017-02-04 23:52:22
【问题描述】:

我正在尝试在我正在构建的网站的后端实现类别创建表单。这个想法是让它与现有类别的索引在同一页面上。但是,目前,创建系统什么也没做。它不会返回错误;直接查询时,数据库中没有任何显示;根据重定向,页面上不会出现任何新内容。因为没有反馈,我正在摸索这条路线与我之前为帖子创建机制所做的非常相似的路线有什么不同/错误。无论如何,这里是相关的创建表格:

<form method="POST" action="{{ route('categories.store') }}" data-parsley-validate>
        <div class="form-group">
          <label name="name">Category Name:</label>
          <input id="name" name="name" class="form-control" required maxlength="255">
        </div>
        <input type="submit" value="Create" class="btn btn-lg btn-block">
        <input type="hidden" name="_token" value="{{ Session::token() }}">
</form>

这是 CategoryController 中的“存储”方法:

public function store(Request $request)
    {
        $this->validate($request, array(
          'name' => 'required|max:255'
        ));

        $category = new Category;
        $category->name = $request->name;
        $category->save();

        Session::flash('success', 'Category has been created!');

        return redirect()->route('categories.index');
    }

这里是 web.php 路由文件:

<?php

Route::get('/', 'PageController@getIndex');
Route::get('/contact', 'PageController@getContact');

Route::resource('posts', 'PostController');
Route::resource('categories', 'CategoryController');
Route::get('blog/{slug}', 'PostController@show')->where('slug', '[\w\d\-\_]+');
Route::get('blog', 'PostController@index');

Auth::routes();

Route::get('/home', 'HomeController@index');

?>

同样,我根本没有收到任何错误消息,只是提交按钮什么也没做。提前致谢!

【问题讨论】:

    标签: php laravel crud


    【解决方案1】:

    试试这个

    return redirect('categories.index')->withErrors($validation);
    

    你也可以检查一下

    Display errors

    【讨论】:

    • 另一件要尝试的事情是将整个事情包装在一个 try catch 中。
    • 以上以及推荐的链接都没有效果。 RE:建议使用 try-catch 语句,我假设想法是将整个当前存储操作包装在 try 子句中?
    【解决方案2】:

    自己发现了错误。在相关视图的顶部,有一个用于显示现有类别的表格。结束标签缺少我忽略的正斜杠。显然,这完全中断了后来出现的表单的功能,而没有引发任何错误。

    故事的寓意是:如果您正在处理网络应用程序上的动态行为并且没有遇到任何异常,则有可能是因为错误在您的 HTML 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-30
      • 2019-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多