title: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null error when assign forien key value?
body: <p>I want to assign foreign key value. foreign key is <code>user_id</code> in <code>p_members</code> table. The original key is userId in <code>all_users</code> table. But when I try to do that occur following error.</p>
  
  <p>I create separate two table to get the user details and get <code>p-member</code> details.</p>
  
  <p>I'm a beginner in Laravel.</p>
  
  <p><strong>Error:</strong></p>
  
  <blockquote>
    <p>SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id'
    cannot be null (SQL: insert into `p_members` (`addNo`, `addStreet`,
    `addCity`, `intentToJoin`, `user_id`, `updated_at`, `created_at`)
    values (qwertfgyhj, ertfyguhj, erdftgyhuj, ertfgyhjk, , 2018-09-06
    16:38:19, 2018-09-06 16:38:19))</p>
  </blockquote>
  
  <p><strong>My controller is:</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use App\allUser;
  use App\ORFOL;
  use App\pMember;
  
  class userController extends Controller
  {
      public function storeUsers(Request $request){
              $table1=new allUser();
  
  
              $table1-&gt;nameWithInitials = $request-&gt;input('name');
              $table1-&gt;callingName      = $request-&gt;input('callingName');
              $table1-&gt;email            = $request-&gt;input('email');
              $table1-&gt;NIC              = $request-&gt;input('nic');
              $table1-&gt;contactNo        = $request-&gt;input('contactNo');
              $table1-&gt;password         = bcrypt($request-&gt;input('password'));
              $table1-&gt;type             = $request-&gt;input('type');
  
              $table1-&gt;save();
  
  
          if($request-&gt;input('type') == 'P-Member'){
              $table2=new pMember();
  
              $table2-&gt;addNo            = $request-&gt;input('addNo');
              $table2-&gt;addStreet        = $request-&gt;input('addStreet');
              $table2-&gt;addCity          = $request-&gt;input('addCity');
              $table2-&gt;intentToJoin     = $request-&gt;input('intentToJoin');
  
              $table2-&gt;user_id          = $table1-&gt;userId;
  
              $table2-&gt;save();
              return view('familyMember');
  
          }
  
          if(($request-&gt;input('type')) =='OR-FOL' ){
              $table3=new ORFOL();
  
              $table3-&gt;region           = $request-&gt;input('region');
              $table3-&gt;user_id          = $table1-&gt;userId;
  
              $table3-&gt;save();
  
          }
  
          if(($request-&gt;input('type')) =='' ){
              $table3=new ORFOL();
  
              $table3-&gt;region           = $request-&gt;input('region');
              $table2-&gt;user_id          = $table1-&gt;userId;
  
              $table3-&gt;save();
  
          }
  
  
          return redirect()-&gt;back();
      }
  }
  </code></pre>