title: Foreign Key not working in Laravel
body: <p>I am creating cms app.</p>
  
  <p>There are two tables - users and posts. Users table have an id of each user. On the other hand, Posts Table has user id which references to the id of users table.</p>
  
  <p>However, while saving the post, I get an error that <code>Field 'user_id' doesn't have a default value</code>. Why is this? Please help me.</p>
  
  <p>Posts Table:</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Support\Facades\Schema;
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;
  
  class CreatePostsTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('posts', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;integer('user_id');
              $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users');
              $table-&gt;string('title');
              $table-&gt;longText('description');
              $table-&gt;timestamps();
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('posts');
      }
  }
  </code></pre>
  
  <p>DashboardController</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers\Dashboard;
  
  use Illuminate\Http\Request;
  use App\Http\Controllers\Controller;
  use App\Post;
  
  class DashboardController extends Controller
  {
      /**
       * Display a listing of the resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function index()
      {
          return view('dashboard.index');
      }
  
      /**
       * Show the form for creating a new resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function create()
      {
          return view('dashboard.create');
      }
  
      /**
       * Store a newly created resource in storage.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return \Illuminate\Http\Response
       */
      public function store(Request $request)
      {
          $post = new Post();
          $post-&gt;title = $request-&gt;title;
          $post-&gt;description = $request-&gt;description;
          $post-&gt;save();
  
          return redirect()-&gt;intended('dashboard');
      }
  
      /**
       * Display the specified resource.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function show($id)
      {
          //
      }
  
      /**
       * Show the form for editing the specified resource.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function edit($id)
      {
          //
      }
  
      /**
       * Update the specified resource in storage.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function update(Request $request, $id)
      {
          //
      }
  
      /**
       * Remove the specified resource from storage.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function destroy($id)
      {
          //
      }
  }
  </code></pre>
  
  <p>Post model:</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Post extends Model
  {
      //
  }
  </code></pre>
  
  <p>User model:</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Notifications\Notifiable;
  use Illuminate\Foundation\Auth\User as Authenticatable;
  
  class User extends Authenticatable
  {
      use Notifiable;
  
      /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
      protected $fillable = [
          'fname', 'lname', 'email', 'password'
      ];
  
      /**
       * The attributes that should be hidden for arrays.
       *
       * @var array
       */
      protected $hidden = [
          'password', 'remember_token',
      ];
  
      public function getRememberToken()
      {
          return $this-&gt;remember_token;
      }
  
      public function setRememberToken($value)
      {
          $this-&gt;remember_token = $value;
      }
  
      public function getRememberTokenName()
      {
          return 'remember_token';
      }
  }
  </code></pre>
  
  <p>Although, I defined a foreign key 'user_id', it doesn't automatically fetch the id from user table. How can I fix this?</p>