[英]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. 我在laravel应用程序中建立了一对一的关系,但是当我尝试引用相关表时,出现了“试图获取非对象的属性”错误。
My Contractor.php model: 我的Contractor.php模型:
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: 我的ContractorProfile.php模型:
<?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 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. 如果我只调用$ contractor-> profile,则页面加载,但没有回显任何内容。 If I add the ->tag_line, I get the "trying to get property of non-object" error. 如果添加-> tag_line,则会收到“尝试获取非对象属性”错误。 tag_line is a column name inside of my contractor_profile table. tag_line是我的contractor_profile表内的列名。
Do you see an error that I am making? 您看到我犯的错误吗?
TIA 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. 问题可能是因为您为Contractor
模型使用了非标准的表名。 Try defining the relationship in your ContractorProfile
model by specifying the second parameter to belongsTo
: 尝试通过将belongsTo
指定为第二个参数来在ContractorProfile
模型中定义关系:
return $this->belongsTo('Contractor', 'contractor_id');
You may also need to perform the same mapping on the Contractor
model as well: 您可能还需要在Contractor
模型上执行相同的映射:
return $this->hasOne('ContractorProfile', 'contractor_id');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.