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

rafidAhsan's avatar

Laravel Field doesn't have a default value

A normal store function doesn't run due to this error.. I dont find the answer

error

SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `single_images` (`description`, `image`, `updated_at`, `created_at`) values (This is orange, 1617553727.jpg, 2021-04-04 16:28:47, 2021-04-04 16:28:47))

Controller

public function store(Request $request) {
        $request->validate([
            'name' => 'required',
            'description' => 'nullable',
            'image' => 'required|image',
        ]);

        // Image move to file
        Storage::putFile('single_image', $request->file('image'));
        $imageName = time().'.'.$request->image->extension();

        SingleImage::create([
            'title' => $request->input('name'),
            'description' => $request->input('description'),
            'user_id' => Auth::user()->id,
            'team_id' => Auth::user()->currentTeam->id,
            'image' => $imageName
        ]);

        return redirect('/dashboard')->with('msg','You have successfully upload image.');
    }
0 likes
6 replies
tykus's avatar

Are the title, user_id and team_id properties fillable on the model; they are not appearing in the INSERT query?

1 like
rafidAhsan's avatar
protected $fillable = [
        'name', 'description', 'image',
    ];

This is my model. why did this happen?

rafidAhsan's avatar

Yeah.. I find the error now... Thank you so much

tykus's avatar

Your column is title or name?

Anyway, the user_id and team_id are not fillable, so:

protected $fillable = [
        'title', 'description', 'image', 'user_id', 'team_id',
    ];
Sergiu17's avatar
Sergiu17
Best Answer
Level 60

SingleImage model, add those fields to $fillable array,

protected $fillable = ['title, 'description', 'user_id', 'team_id', 'image'];

Please or to participate in this conversation.