简体   繁体   中英

Laravel Eloquent one-to-one relationship setup

I am setting up a one to one relationship in a laravel application, but I am getting the error "trying to get property of non-object" when I try to reference a related table.

My Contractor.php model:

    class Contractor extends Eloquent  {

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'contractor';

function profile() {
    return $this->hasOne('ContractorProfile');
}
}

My ContractorProfile.php model:

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class ContractorProfile extends Eloquent  {

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'contractor_profile';

 public function contractor() {
    return $this->belongsTo('Contractor');
 }
}

here is the snippet of my view file

show.blade.php

<div>
 <h4>{{ $contractor->profile->tag_line }}</h4></p>
</div>

If I just call $contractor->profile the page loads but nothing is echoed back. If I add the ->tag_line, I get the "trying to get property of non-object" error. tag_line is a column name inside of my contractor_profile table.

Do you see an error that I am making?

TIA

EDIT: Database info:

Schema::create('contractor', function ($table) {

        $table->increments('id');
        $table->integer('hba_number')->nullable();
        $table->integer('msn')->nullable();
        $table->string('type')->default("");
        $table->string('name')->default("");
        $table->string('address_1')->default("");
        $table->string('address_2')->default("");
        $table->string('city')->default("");
        $table->string('state')->default("");
        $table->string('zip')->default("");
        $table->string('country')->default("");
        $table->string('phone')->default("");
        $table->string('website')->default("");
        $table->integer('company_id')->nullable();
        $table->integer('user_id');
        $table->timestamps();
        $table->softDeletes();
    });

Schema::create('contractor_profile', function ($table) {

        $table->increments('id');
        $table->integer('contractor_id');

        $table->string('tag_line')->default("");
        $table->string('story')->default("");
        $table->string('area_of_operation')->default("");
        $table->text('experience')->default("");
        $table->text('education')->default("");
        $table->text('insurance_verified')->default("");
        $table->timestamps();
        $table->softDeletes();
    });

The issue may be because you're using a non-standard table name for your Contractor model. Try defining the relationship in your ContractorProfile model by specifying the second parameter to belongsTo :

return $this->belongsTo('Contractor', 'contractor_id');

You may also need to perform the same mapping on the Contractor model as well:

return $this->hasOne('ContractorProfile', 'contractor_id');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM