【问题标题】:SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null - LaravelSQLSTATE [23000]:违反完整性约束:1048 列“标题”不能为空 - Laravel
【发布时间】:2020-08-07 07:27:02
【问题描述】:

我已经发表了一些关于 laravel 的帖子,但这绝对是我遇到的最困难的一个。我正在为自己制作一个博客应用程序来学习 Laravel,在我更改文件结构之前,它运行得非常流畅:

-[views]
--[blogs]
---index.blade.php
---create.blade.php
---show.blade.php

收件人:

-[views]
--index.blade.php
--create.blade.php
--[admin]
---show.blade.php

这是我的路线文件:

Route::get('/', 'Blogs@index')->name('blogs_path');
Route::get('/admin/create', 'Blogs@create')->name('create_blogs_path');
Route::get('/', 'Blogs@store')->name('store_blogs_path');
Route::get('/{id}', 'Blogs@show')->name('blog_path');

这是我的博客控制器文件:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Blog;

class Blogs extends Controller
{
    //

    public function index(){

        $blogs = Blog::all();

        return view('index', ['blogs' => $blogs]);
    }

    public function show($id){

        $blog = Blog::find($id);

        return view('show', ['blog' => $blog]);
    }

    public function create(){

        return view('admin.create');
    }

    public function store(Request $request){

        $blog = new Blog;

        $blog->title = $request->title;
        $blog->content = $request->content;
        $blog->created_at = $request->created_at;

        $blog->save();

        return redirect()->route('blogs_path');
    }
}

它在旧的文件结构中完全可以正常工作,但是当我将它恢复到一个级别时,一切都崩溃了。这是我得到的错误:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null (SQL: insert into 'blogs' ('title', 'content', 'created_at', 'updated_at') values (?, ?, ?, 2020-04-23 20:10:20))

但我在表格中有数据,并且之前工作得非常好。

谢谢。

【问题讨论】:

  • 你试过了吗php artisan optimize:clear
  • 刚刚做了,没用。
  • 请给我看dd($request-&gt;all())结果
  • 我不能把它放在任何地方,因为我的文件由于错误而无法加载。
  • 这条路线Route::get('/', 'Blogs@store')...应该有post方法,而不是getRoute::post('/', 'Blogs@store')...

标签: php mysql laravel


【解决方案1】:

/ 路由重复两次:

Route::get('/', 'Blogs@index')->name('blogs_path');
...
Route::get('/', 'Blogs@store')->name('store_blogs_path');
...

修复它应该可以工作。

【讨论】:

    猜你喜欢
    • 2020-02-11
    • 2021-02-09
    • 2021-02-01
    • 2017-07-31
    • 2019-11-20
    • 2021-08-16
    • 2019-10-06
    • 2020-10-25
    • 2017-02-28
    相关资源
    最近更新 更多