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

Michael Fayez's avatar

what's missing in this code

General error: 1364 Field 'user_id' doesn't have a default value I got this error don't know what is the problem

<?php

namespace App\Http\Livewire\MarketResearch;

use App\Models\MarketResearch;
use Livewire\Component;

class Create extends Component
{

    public MarketResearch $marketResearch;

    public function mount(MarketResearch $marketResearch)
    {
        $this->marketResearch = $marketResearch;
    }

    public function render()
    {
        return view('livewire.market-research.create');
    }

    public function submit()
    {
        $this->validate();

        $this->marketResearch->save();

        return redirect()->route('admin.market-researchs.index');
    }

    protected function rules(): array
    {
        return [
            'marketResearch.title' => [
                'string',
                'nullable',
            ],
            'marketResearch.details' => [
                'string',
                'nullable',
            ],
            'marketResearch.user_id' => [
                'string',
                'nullable',
            ],

        ];
    }


}
0 likes
2 replies
LaryAI's avatar
Level 58

The error message you're seeing indicates that when you're trying to save a MarketResearch model, the user_id field is not being set, and since it doesn't have a default value in the database, the operation fails.

To resolve this issue, you need to ensure that the user_id field is being set before you attempt to save the MarketResearch model. Here's how you can modify your submit method to include the user_id:

public function submit()
{
    $this->validate();

    // Assuming you want to associate the market research with the currently authenticated user
    $this->marketResearch->user_id = auth()->id();

    $this->marketResearch->save();

    return redirect()->route('admin.market-researchs.index');
}

In the above code, auth()->id() is used to get the ID of the currently authenticated user and assign it to the user_id field of the MarketResearch model before saving it.

Additionally, make sure that the user_id field is fillable in your MarketResearch model. You can do this by adding it to the $fillable array in the model:

class MarketResearch extends Model
{
    protected $fillable = [
        // ... other fields ...
        'user_id',
        // ... other fields ...
    ];

    // ...
}

This will allow mass assignment on the user_id field when you're creating or updating a MarketResearch instance.

Lastly, ensure that the user_id field is correctly defined in your database migration for the market_researches table and that it references the id field on the users table if it's supposed to be a foreign key.

Here's an example of how the migration might look:

Schema::create('market_researches', function (Blueprint $table) {
    $table->id();
    // ... other columns ...
    $table->unsignedBigInteger('user_id');
    $table->foreign('user_id')->references('id')->on('users');
    // ... other columns ...
});

Make sure to run your migrations if you make any changes to them.

JabatoForever's avatar

@michael fayez the problem is that u should make the user_id nullable in your market_researches table create a migration to update the user_id column in the market_researches table like so

php artisan make:migration change_user_id_column_in_market_researches_table

in the migration then

 Schema::table('market_researches', function (Blueprint $table) {
     $table->foreignId('user_id')->nullable()->change();
 });

now when u save the record the user_id filed should be settable to null.

Please or to participate in this conversation.