laravel-5.7:数据未保存到数据库中

laravel-5.7:数据未保存到数据库中,laravel,laravel-5.7,Laravel,Laravel 5.7,嗨,我正在尝试将数据保存到数据库中,但它只返回下一页,而不将数据保存到数据库中,是否有解决方案来解决此问题 控制器: public function create(Request $request) { if ($request->isMethod('post')) { $data = $request->all(); $categories = new Categories; $categories->name = $data['ca

嗨,我正在尝试将数据保存到数据库中,但它只返回下一页,而不将数据保存到数据库中,是否有解决方案来解决此问题

控制器:

  public function create(Request $request)
  {

    if ($request->isMethod('post')) {
    $data = $request->all();
    $categories = new Categories;
    $categories->name = $data['category_name'];
    $categories_description = $data['description'];
    $categories->save();

       return view('admin.categories.index');
    }

    return view('admin.categories.create');
  }
刀片文件:

  <form class="k-form" method="post" action="{{ url('/admin/categories/index')}}" name="" id="k_form">@csrf
                <div class="row">

                                    <label class="col-3 col-form-label">Category Name:</label>
                                    <div class="col-9">
                                        <input class="form-control" type="text" name="category_name" value="" required="required">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label class="col-3 col-form-label">Description:</label>
                                    <div class="col-9">
                                        <textarea name="description" required="required"></textarea>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label class="col-3 col-form-label"></label>
                                    <div class="col-9">
                                        <div class="k-checkbox-single">
                                            <input type="submit" value="Add Category" style="background-color: #5867dd; color: #fff; border-radius: 5px; padding: 10px;" name="">
                                        </div>

            </form>
你搞错了

$categories_description = $data['description'];
应该是

$categories->description = $data['description'];
$categories = new Category;
希望这有帮助:)

你错在这里了

$categories_description = $data['description'];
应该是

$categories->description = $data['description'];
$categories = new Category;
希望这有帮助:)

问题出在

 $categories = new Categories;
我认为应该是这样

$categories->description = $data['description'];
$categories = new Category;

模型为单数形式,其中as表根据laravel命名约定为复数形式

此外,如果要批量分配值,请将列名添加到可填充数组中。像

protected $fillable = ['name', 'description'];
另外,请确保您的模型位于应用程序文件夹中,并且您没有创建任何子目录,例如“App\Models”或其他内容。

问题出在应用程序文件夹中

 $categories = new Categories;
我认为应该是这样

$categories->description = $data['description'];
$categories = new Category;

模型为单数形式,其中as表根据laravel命名约定为复数形式

此外,如果要批量分配值,请将列名添加到可填充数组中。像

protected $fillable = ['name', 'description'];


另外,请确保您的模型位于应用程序文件夹中,并且您没有创建任何子目录,如“App\Models”或其他内容。

您可以发布模型吗?action=“{url('/admin/categories/create')}”@aimme model is emptymodel应该有“`protected$guarded=[];`你能检查一下吗?@dparoli Class'App\Http\Controllers\Categories'没有找到,错误行是:$Categories=newcategories;你能发布模型吗?action=“{url('/admin/categories/create')}”@aimme模型为空模型应该有`protected$guarded=[];`你能检查一下吗?@dparoli Class'App\Http\Controllers\Categories'没有找到,错误行是:$Categories=newcategories;我不确定,但尝试使用另一个方法名。你会建议任何方法吗?默认情况下,laravel会使用create方法作为视图的返回,并使用store方法将数据存储在db中。你能将表名放在模型中吗?现在我将控制器转换为store方法,但它仍然显示相同的结果,我应该调用route中的store方法吗?是,你应该从route by post方法调用store方法。我不确定,但尝试使用另一个方法名称。你会建议任何方法吗?默认情况下,laravel会将create方法作为返回视图的方法,并将store方法存储在db中。你能将表名放在模型中吗?现在我将控制器转换为store方法,但是它仍然显示相同的结果,我应该调用route中的store方法吗?是的,您应该从route逐post方法调用store方法。我猜您的表单提交根本不是使用正确的方法。添加dd($request->all());就在if条件之后。让我知道结果。。基于此,我可以帮助你。以下是你能做的,1。在web.php(例如:categories.store)2中命名路由。以如下形式更改操作:action=“{route('categories.store)}}”3。在控制器中,将保存代码从创建方法移动到存储方法。在这里,您不必验证请求是否是post。我猜您的表单提交根本没有使用正确的方法。添加dd($request->all());就在if条件之后。让我知道结果。。基于此,我可以帮助你。以下是你能做的,1。在web.php(例如:categories.store)2中命名路由。以如下形式更改操作:action=“{route('categories.store)}}”3。在控制器中,将保存代码从创建方法移动到存储方法。在这里,您不必验证请求是否为post。