Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jacklinwood's avatar

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'price' cannot be null

I'm having issues setting the default value of a column in my migration file.

Migration:

$table->decimal('price', 15, 2)->default(0);

Model:

protected $fillable = ['product_id', 'option_id', 'option_value_id', 'price', 'price_prefix', 'weight', 'weight_prefix', 'sku'];

I can definitely see that the default value is there in my Sequel Pro database. But I still receive the error shown in the title when not giving a value.

I'm using Laravel 8.5.0

Any ideas?

0 likes
13 replies
MichalOravec's avatar

I think it should be

$table->decimal('price', 15, 2)->default(0.00);
Talinon's avatar

@jacklinwood You're certain you're actually hitting your Sequel Pro database?

I've seen a lot of cases where people realized they had another database set within the configuration, such as a test database.

You can quickly test by typing within tinker: config('database.default')

jacklinwood's avatar

@michaloravec

Thanks for the reply. I tried this too and still having the same issue!

@talinon

Thanks for the reply. I am definitely sure I'm hitting the right database. The rest of my app is working fine plus it works if I do give a price input.

GeordieJackson's avatar

Have you tried

->default("0")

i.e. add quotation marks to ensure that the 0 is saved as a string.

Talinon's avatar

@jacklinwood Would you happen to have a mutator on the model that is causing it to be null?

As a test, what happens if you temporarily comment out ConvertEmptyStringsToNull within your app/Http/Kernel.php file?

// \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class

I'm thinking you might be sending something as a string and it's getting converted to null.

jacklinwood's avatar

Nothing that is affecting the price:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use GrahamCampbell\Markdown\Facades\Markdown;

class Product extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
    	'name', 'model', 'description', 'sku', 'mpn', 'quantity', 'stock_status_id', 'manufacturer_id', 'shipping', 'price', 'tax_class_id', 'date_available', 'weight', 'meta_title', 'meta_description', 'meta_keywords'
    ];

    public function productOptions()
    {
        return $this->hasMany('App\Models\ProductOption');
    }

    public function getDescriptionAsHtmlAttribute()
    {
        return Markdown::convertToHtml($this->description);
    }
}
GeordieJackson's avatar

I've just tried this with Laravel 8 using MySQL 8 and it works fine.

Could it be your database settings rather than a problem with the code?

Talinon's avatar

@geordiejackson Did you try disabling ConvertEmptyStringsToNull as an experiment?

Have you opened developer tools within the browser and inspected what is actually sent in the request?

Talinon's avatar

@geordiejackson Since you are including the HasFactory trait, I assume you have a factory for the model. Another test you could try is something like this within tinker:


YourModel::factory(['price' => 0])->create();   // this should create a row in your database table

$model = YourModel::factory()->make();

unset($model->price); // to make sure its not set

$model->save();   // if this works, then there is nothing wrong with your database. It must be your application request


GeordieJackson's avatar

I've had another look at this and can't see anything wrong.

In Tinker I did:

$p = App\Models\Product::factory()->make();

and got

App\Models\Product {#4362
     name: "doloribus",
     price: 0,
   }

then

>>> unset($p->price);
>>> $p
=> App\Models\Product {#4362
     name: "doloribus",
   }
>>> $p->save();
=> true
>>> $p
=> App\Models\Product {#4362
     name: "doloribus",
     updated_at: "2020-10-29 15:00:39",
     created_at: "2020-10-29 15:00:39",
     id: 17,
   }
>>> $p->refresh();
=> App\Models\Product {#4362
     id: 17,
     name: "doloribus",
     price: "0.00",
     created_at: "2020-10-29 15:00:39",
     updated_at: "2020-10-29 15:00:39",
   }

at the save() stage, the database set the price to "0.00" as expected.

varan's avatar

i have same issue with laravel 8 and mysql

varan's avatar

I was forced to make column nullable

TassosVallas's avatar

I know it's been long ago, but I came across a similar situation and I managed to solve it like this. In your controller, in your store method, after the validations:

$data = $request->all();

    if($data['price'] === null){
        $data['price'] = 0;
    }

Yourmodel::create($data);

Just in case someone can be helped.

Please or to participate in this conversation.