Praveesh
Praveesh

Reputation: 1327

Laravel-5 saving entity with multiple relations

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.

In Project Model:

/**
 * Fetch the Staff entity associated with a project
 *
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function projectLead()
{
    return $this->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->belongsTo('App\Client','client_id');
}

In Staff Model:

 public function leadProjects()
  {
    return $this->hasMany('App\Project','project_leader_id');
  }

In Client Model:

private function projects()
   {
    return $this->hasMany('App\Project');
   }

My ProjectController action for saving the project:

public function store( CreateProjectRequest $request)
{
    $input = $request->all();


    $project = new Project();

    $teamLead = Staff::find($input['project_lead_id']);
    $client = Client::find($input['project_lead_id']);

    $project->name = $input['name'];
    $project->active = $input['active'];
    $project->date_setup = $input['date_setup'];
    $project->status = $input['status'];
    $project->hourly_rate_default = $input['hourly_rate_default'];
    $project->projectLead()->associate($teamLead);
    $project->client()->associate($client);

    $project->save();

}

Migration:

 Schema::create('project', function(Blueprint $table)
    {
        $table->engine = 'InnoDB';

       $table->integer('project_id', true);
        $table->string('name', 100);
        $table->boolean('active');
        $table->date('date_setup');
        $table->boolean('status');
       $table->float('hourly_rate_default', 10, 0);
        $table->integer('project_leader_id');

        $table->foreign('project_leader_id')
            ->references('staff_id')->on('staff')->onDelete('cascade');
        $table->timestamps();
        $table->integer('client_id');
       $table->foreign('client_id')
            ->references('client_id')->on('client')->onDelete('cascade');

    });

However, when I am trying to save a new project, I am getting error.

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":"[email protected]","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))

I've been stuck on this error for a pretty decent amount of time. Any help is highly appreciated.

Upvotes: 1

Views: 1802

Answers (1)

kamlesh.bar
kamlesh.bar

Reputation: 1804

In Staff Model change to this

 public function leadProjects()
  {
    return $this->hasMany('App\Project','project_leader_id','staff_id');
  }

In Project Model

public function projectLead()
{
    return $this->belongsTo('App\Staff','project_leader_id','staff_id');
}

Upvotes: 1

Related Questions