title: Laravel-5 saving entity with multiple relations
body: <p>I have three entities Project, Staff, and Client in Laravel 5 project. A Project has many to one relation with Client and Staff.
  I have declared the relations as given below.</p>
  
  <p>In Project Model:</p>
  
  <pre><code>/**
   * Fetch the Staff entity associated with a project
   *
   * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
   */
  public function projectLead()
  {
      return $this-&gt;belongsTo('App\Staff','project_leader_id');
  }
  
  /**
   * Fetch the Client entity associated with a project
   *
   * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
   */
  public function client()
  {
      return $this-&gt;belongsTo('App\Client','client_id');
  }
  </code></pre>
  
  <p>In Staff Model:</p>
  
  <pre><code> public function leadProjects()
    {
      return $this-&gt;hasMany('App\Project','project_leader_id');
    }
  </code></pre>
  
  <p>In Client Model:</p>
  
  <pre><code>private function projects()
     {
      return $this-&gt;hasMany('App\Project');
     }
  </code></pre>
  
  <p>My ProjectController action for saving the project:</p>
  
  <pre><code>public function store( CreateProjectRequest $request)
  {
      $input = $request-&gt;all();
  
  
      $project = new Project();
  
      $teamLead = Staff::find($input['project_lead_id']);
      $client = Client::find($input['project_lead_id']);
  
      $project-&gt;name = $input['name'];
      $project-&gt;active = $input['active'];
      $project-&gt;date_setup = $input['date_setup'];
      $project-&gt;status = $input['status'];
      $project-&gt;hourly_rate_default = $input['hourly_rate_default'];
      $project-&gt;projectLead()-&gt;associate($teamLead);
      $project-&gt;client()-&gt;associate($client);
  
      $project-&gt;save();
  
  }
  </code></pre>
  
  <p>Migration:</p>
  
  <pre><code> Schema::create('project', function(Blueprint $table)
      {
          $table-&gt;engine = 'InnoDB';
  
         $table-&gt;integer('project_id', true);
          $table-&gt;string('name', 100);
          $table-&gt;boolean('active');
          $table-&gt;date('date_setup');
          $table-&gt;boolean('status');
         $table-&gt;float('hourly_rate_default', 10, 0);
          $table-&gt;integer('project_leader_id');
  
          $table-&gt;foreign('project_leader_id')
              -&gt;references('staff_id')-&gt;on('staff')-&gt;onDelete('cascade');
          $table-&gt;timestamps();
          $table-&gt;integer('client_id');
         $table-&gt;foreign('client_id')
              -&gt;references('client_id')-&gt;on('client')-&gt;onDelete('cascade');
  
      });
  </code></pre>
  
  <p>However, when I am trying to save a new project, I am getting error.</p>
  
  <pre><code>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a        child row: a foreign key constraint fails (`erplite_portal`.`project`, CONSTRAINT `project_project_leader_id_foreign` FOREIGN KEY (`project_leader_id`) REFERENCES `staff` (`staff_id`) ON DELETE CASCADE) (SQL: insert into `project` (`name`, `active`, `date_setup`, `status`, `hourly_rate_default`, `project_leader_id`, `client_id`, `updated_at`, `created_at`) values (Aida Shaghayegh Irajinia, 1, 12-01-81, 2, 12, {"staff_id":"1","name":"Praveesh A","date_joined":"2015-04-01","date_resigned":"0000-00-00","staff_role":"development","password":"praveesh","email":"praveesh@lbit.in","is_master":"1","phone":"9446814101","phone_alternate":"9448162992","profile_picture":"","active":"1"}, {"client_id":"1","active":"1","name":"Lbit","address":"Lbit Ekm","country_id":"91","phone":"9446814101","date_registered":"2015-04-01","project_lead_id":"1","billing_account_id":"1","credit_limit":"100","invoice_day":"0","invoice_due_days":"15","require_prepayment":"1","deduct_cc_fee_percentage":"0","hourly_rate":"150","currency_id":"91","recommended_prepayment_hours":"15","invoice_automatically":"1","invoice_projects_together":"1","invoice_representative_id":"1"}, 2015-04-27 01:44:13, 2015-04-27 01:44:13))
  </code></pre>
  
  <p>I've been stuck on this error for a pretty decent amount of time. Any help is highly appreciated.</p>