1

I want to create instance using Laravel 5 Eloquent Relationship.

I have 2 Migrations and 2 Eloquent Model.

Companies Migration:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCompaniesTable extends Migration {

    public function up()
    {
        Schema::create('Companies', function(Blueprint $table)
        {
            $table->string('CompanyCode', 15);
            $table->string('Name', 200);
            $table->string('Type', 100);
            $table->tinyInteger('IsActive')->default(1);
            $table->timestamps();

            $table->primary('CompanyCode');
        });
    }

    public function down()
    {
        Schema::drop('Companies');
    }

}

Company Model:

namespace App\Models\Setting\Organization;

use Illuminate\Database\Eloquent\Model;

class Company extends Model {

    protected $fillable = ['CompanyCode', 'Name', 'Type'];

    public function organizationUnits(){
        return $this->hasMany('App\Models\Setting\Organization\OrganizationUnit', 'CompanyCode');
    }
}

OrganizationUnits Migration:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateOrganizationUnitsTable extends Migration {

    public function up()
    {
        Schema::create('OrganizationUnits', function(Blueprint $table)
        {
            $table->string('OrganizationUnitCode', 15); //PK
            $table->string('CompanyCode', 15); //FK
            $table->string('Name', 200);
            $table->tinyInteger('IsActive')->default(1);
            $table->timestamps();

            $table->primary('OrganizationUnitCode');
            $table->foreign('CompanyCode', 'OrgUnits_Company_FK')
                  ->references('CompanyCode')
                  ->on('Companies')
                  ->onDelete('cascade');
        });

    }

    public function down()
    {
        Schema::drop('OrganizationUnits');
    }

}

OrganizationUnit Model:

namespace App\Models\Setting\Organization;

use Illuminate\Database\Eloquent\Model;

class OrganizationUnit extends Model {

    protected $table = "OrganizationUnits";

    protected $fillable = ['OrganizationUnitCode', 'CompanyCode', 'Name'];

    public function company(){
        return $this->belongsTo('App\Models\Setting\Organization\Company', 'CompanyCode');
    }

}

The relationship is one Company may have one or more OrganizationUnit, one OrganizationUnit must have one and only one Company.

I tried to create new instance of OrganizationUnit in php artisan tinker using this code:

$company = \App\Models\Setting\Organization\Company::first();
$orgunit = $company->organizationUnits()->create(['OrganizationUnitCode' => 'abcdef']);

But Laravel gives the following error:

Illuminate\Database\QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'CompanyCode' cannot be null (SQL: insert into `OrganizationUnits` (`Org anizationUnitCode`, `CompanyCode`, `updated_at`, `created_at`) values (abcdef, , 2015-12-17 00:17:33, 2015-12-17 00:17:33))'

Where did I go wrong? Please help. I'm new to Laravel.

1 Answer 1

1

It clearly says that CompanyCode cannot be null. You can define it by hand or you can use the increments method on the Blueprint instance while creating your migration.

Sign up to request clarification or add additional context in comments.

1 Comment

Hello @OzanKurt , thanks for the answer. I already tried using default value " " to the Foreign Key field in OrganizationUnits table, but laravel gives foreign key constraint error since there's no " " CompanyCode in Companies table. $table->string('CompanyCode', 15)->default(" "); //FK

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.