title: Laravel insert FOREIGN KEY in Form Request method
body: <p>In my application some tables are relational and I need to insert <code>FOREIGN KEY</code> to the table as User id. I don't have any problem with creating a new instance from the model and then use the <code>save()</code> method. However I would like to use the <code>Form Request</code> method to create and update records.</p>
  
  <p>I get this error, but I don't know how to solve it;</p>
  
  <blockquote>
    <p>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a >child row: a foreign key constraint fails (<code>epaypro</code>.<code>merchand_web_service</code>, >CONSTRAINT <code>merchand_web_service_user_id_foreign</code> FOREIGN KEY (<code>user_id</code>) >REFERENCES <code>users</code> (<code>id</code>)) (SQL: insert into <code>merchand_web_service</code> >(<code>updated_at</code>, <code>created_at</code>) values (2015-11-29 11:31:54, 2015-11-29 11:31:54))</p>
  </blockquote>
  
  <p>This is my model:</p>
  
  <pre><code>namespace app;
  use Illuminate\Database\Eloquent\Model;
  use Morilog\Jalali\jDate;
  
  class Merchant extends Model
  {
      protected $table = 'merchand_web_service';
      protected $fillable = ['customer_key', 'company_name', 'company_logo'];
  
      /**
       * @return mixed
       */
      public function user()
      {
          return $this-&gt;belongsTo('App\User');
      }
  }
  </code></pre>
  
  <p><code>store</code> method on Controller:</p>
  
  <pre><code>public function store(StoreMerchantWebServiceRequest $request)
  {
      Merchant::create($request-&gt;all());
      return redirect()-&gt;route('post.index');
  }
  </code></pre>
  
  <p><code>StoreMerchantWebServiceRequest</code> class:</p>
  
  <pre><code>class StoreMerchantWebServiceRequest extends Request
  {
      public function authorize()
      {
          if (Auth::check()) {
              return true;
          } else
              return false;
      }
  
      public function forbiddenResponse()
      {
          return Response::make(trans('message.permission_denied'), 403);
      }
  
      public function rules()
      {
          return [
              'agent_company_name' =&gt; 'required',
              'agent_company_logo' =&gt; 'mimes:jpg,jpeg,bmp,png|max:300',
          ];
      }
  }
  </code></pre>
  
  <p>My Test (in controller):</p>
  
  <pre><code>public function store(StoreMerchantWebServiceRequest $request)
  {
      $order = Merchant::create($request-&gt;all());
      $this-&gt;user_id = Auth::user()-&gt;id;
      $order-&gt;save();
      return redirect()-&gt;route('post.index');
  }
  </code></pre>