laravel sql error: SQLSTATE [HY000]: General error: 1364 Field 'user_id' does not have a default value

Asked 2 years ago, Updated 2 years ago, 123 views

I am currently making a task list in laravel.
I added the user_id column, but the error below appears.
I would like you to tell me the possible cause.
I don't know which source code to put in the source code.
Thank you for your cooperation.

SQLSTATE [HY000]: General error: 1364 Field 'user_id' does not have a default value (SQL: insert into `tasks` (`status`, `content`, `updated_at`, `created_at`)

Task.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Task extensions Model
{
    protected$fillable=['content', 'status', 'user_id'];

    public function user()
    {
        return$this->belongsTo(User::class);
    }
}

TasksController

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Task;

class TasksController extensions Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return\Illuminate\Http\Response
     */
    public function index()
    {
        $data=[];
        if(\Auth::check()){
            $user=\Auth::user();
            $tasks=$user->tasks()->orderBy('created_at','desc')>paginate(10);

            $data=[
                'user' = > $user,
                'tasks' = > $tasks,
            ];

            return view('tasks.index',[
                'tasks' = > $tasks,
            ]);

        } else{
            return view('welcome');    
        }

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return\Illuminate\Http\Response
     */
    public function create()
    {
        $task = new Task;

        return view('tasks.create',[
            'task' = > $task,
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param\Illuminate\Http\Request$request
     * @return\Illuminate\Http\Response
     */
    public function store(Request$request)
    {

        $this->validate($request,[
            'status' = > 'required | max:10',
            'content' = > 'required | max:191',
        ]);

        $task = new Task;
        $task->status=$request->status;
        $task->content=$request->content;
        $task->save();

        return redirect('/');
    }

    /**
     * Display the specified resource.
     *
     * @param int$id
     * @return\Illuminate\Http\Response
     */
    public function show($id)
    {
        $task=Task::find($id);

        return view('tasks.show',[
            'task' = > $task,
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int$id
     * @return\Illuminate\Http\Response
     */
    public function edit($id)
    {
        $task=Task::find($id);

        return view('tasks.edit',[
            'task' = > $task,
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param\Illuminate\Http\Request$request
     * @param int$id
     * @return\Illuminate\Http\Response
     */
    public function update(Request$request,$id)
    {

        $this->validate($request,[
            'status' = > 'required | max:10',
            'content' = > 'required | max:191',
        ]);

        $task=Task::find($id);
        $task->status=$request->status;
        $task->content=$request->content;
        $task->save();

        return redirect('/');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int$id
     * @return\Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $task=Task::find($id);
        $task->delete();

        return redirect('/');
    }
}

laravel

2022-09-30 21:46

1 Answers

As far as the error is concerned, user_id is not null column
I think it's an error because the default value is not set even though it doesn't have a default value.

The fix depends on whether user_id allows null or not

Modify migration to allow null value in migration if allowed

If you don't accept it, you need to pass the user_id to the code in laravel when creating the model


2022-09-30 21:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.