title: "SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into
body: <p>i have got this error while working with laravel. I am trying to save a book data and save its owner user with user_id. Sorry about question format.</p>
  
  <pre><code>public function store(InfoValid $request)
  {
      $request-&gt;validated();
      $user = Auth::user()-&gt;books()-&gt;create($request-&gt;except('_token'));
      //dd($user -&gt;user_id);
      Book::create($request-&gt;except('_token'));
      redirect("/books");
  }
  }
  </code></pre>
  
  <blockquote>
    <p>"SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into <code>books</code> (<code>name</code>, <code>page</code>, <code>ISBN</code>, <code>price</code>, <code>published_at</code>, <code>updated_at</code>, <code>created_at</code>) values)) ◀"</p>
  </blockquote>
  
  <p>in my User class i have:</p>
  
  <pre><code>public function books(){
      return $this-&gt;hasMany(Book::class);
  }
  </code></pre>
  
  <p>and in my Book class:</p>
  
  <pre><code>protected $fillable = [
      "name" , "page" , "ISBN" , "price" , "published_at"
  ];
  
  public function user()
  {
  return $this -&gt;belongsTo(User::class);
  }
  </code></pre>
  
  <p>and in my books table:</p>
  
  <pre><code>$table-&gt;unsignedInteger("user_id");
  
          $table-&gt;foreign('user_id')
              -&gt;references('id')
              -&gt;on('users')
              -&gt;onDelete('cascade')
              -&gt;onUpdate('cascade');
  </code></pre>
  
  <p>When i dd() the user_id(i have commented that) it gives me the user_id but i don't know why i'm facing this error</p>