skip to Main Content

Hello I faced the following error:

SQLSTATE[HY000]: General error: 1364 Field ‘title’ doesn’t have a default value.

It happens when I try to update amount of views on specific post.
I’ve just set default value of points while initializing it specified in the model in $attributes table.

Posts table migration:

public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->longText('text');
            $table->integer('points');
            $table->bigInteger('views');
            $table->integer('user_id')->unsigned()->nullable();
            $table->integer('is_closed');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('set null');
        });
    }

Post model:

    const POINTS = 0;
    const VIEWS = 0;
    const IS_CLOSED = 0;

    protected $attributes = [
        'points' => self::POINTS,
        'views' => self::VIEWS,
        'is_closed' => self::IS_CLOSED,
        'title' => null,
        'text' => null,
        'user_id' => null,
    ];

    protected $fillable = [
        'title',
        'text',
        'user_id',
    ];

My Service where I try to increment the value of views:

public function incrementPostViews($id)
    {
        $post = $this->post->findOrFail($id);
        $post->views++;
        return $post->save();
    }

I did use the Request rule but only while creating new post:

public function rules()
    {
        return [
            'title' => ['required', 'max:50'],
            'text' => ['required', 'max:1000'],
            'user_id' => ['numeric', 'nullable'],
        ];
    }

So by default the points, views and is_closed fields are set to 0 while creating new Post. To be honest I do not have any ideas why it is causing an error.

Update:

In Post model I’ve changed the $attributes array and added title, text and user_id which default value is set to null. Those three fields stays in $fillable array also. I’m not sure if it’s the right way to fix it. If it’s not, please correct me.

In the migration there are not changes made.

All changes are visible above.

2

Answers


  1. You declared your title item as required in your table but you didn’t declare a default value. And your insert operation doesn’t give a value for that column, so it fails.

    The easiest way to fix this is probably to declare title as nullable instead.

    But if you have a good reason to make it required, you’ll have to revisit the way you insert your rows so you can offer a value for that column.

    Login or Signup to reply.
  2. You must create THE post first, once that post has been created you can then update its attributes.

     $user = Post::create([
            'title' => $request->title,
            'text' => $request->text,
            'points' => 0,
            'views' => 0,
            'is_closed' => $request->is_closed,
           
        ]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search