Laravel RelationShip 选择字段无效
Laravel RelationShip Selecting Fileds Not Working
我正在尝试从表之间的关系中获取属性,但它似乎不起作用,我尝试了网络上的其他解决方案但仍然不起作用,这里是我调用的代码:
$data = Employer::with(['contratClient'=>function($query){$query->select("idcontrat");}])
->where("id_entre",1)
->get();
echo "<pre>";
dd($data[0]->contratClient);
echo "</pre>";
雇主模型:
class Employer extends Model{
protected $primaryKey = 'id_em';
protected $table = 'employers';
protected $guarded = array();
public function contratClient(){
return $this->hasMany("App\ContratClient","id_emp");
}
}
ContratClient 模型:
class ContratClient extends Model
{
protected $primaryKey = 'idcontrat';
protected $table = 'contratClients';
public function employer(){
return $this->belongsTo('App\Employer','id_em');
}
}
结果如下:
如果我遗漏了什么,请列出,我仍然是 Laravel
的初学者
谢谢
我认为您的主键不是 laravel 所需要的。例如:
在 employers
table 中,laravel 需要 employer_id
但您的主键是 id_em
。请将您的关系更新为以下并再次检查:
class Employer extends Model{
protected $primaryKey = 'id_em';
protected $table = 'employers';
protected $guarded = array();
public function contratClient(){
return $this->hasMany('App\ContratClient','foreign_key_in_this_table', 'idcontrat');
}
}
class ContratClient extends Model
{
protected $primaryKey = 'idcontrat';
protected $table = 'contratClients';
public function employer(){
return $this->belongsTo('App\Employer','foreign_key_in_this_table', 'id_em');
}
}
还要检查它是否是 contratClients
类型,您的意思是 contractClients
吗?
模型关系应该是:
class Employer extends Model{
protected $primaryKey = 'id_em';
protected $table = 'employers';
protected $guarded = array();
public function contratClient(){
return $this->hasMany("App\ContratClient","id_emp", "id_em");
}
}
class ContratClient extends Model
{
protected $primaryKey = 'idcontrat';
protected $table = 'contratClients';
public function employer(){
return $this->belongsTo('App\Employer','id_emp', 'id_em');
}
}
要验证关系,首先尝试获取行:
$data = Employer::with('contratClient')->get();
我正在尝试从表之间的关系中获取属性,但它似乎不起作用,我尝试了网络上的其他解决方案但仍然不起作用,这里是我调用的代码:
$data = Employer::with(['contratClient'=>function($query){$query->select("idcontrat");}])
->where("id_entre",1)
->get();
echo "<pre>";
dd($data[0]->contratClient);
echo "</pre>";
雇主模型:
class Employer extends Model{
protected $primaryKey = 'id_em';
protected $table = 'employers';
protected $guarded = array();
public function contratClient(){
return $this->hasMany("App\ContratClient","id_emp");
}
}
ContratClient 模型:
class ContratClient extends Model
{
protected $primaryKey = 'idcontrat';
protected $table = 'contratClients';
public function employer(){
return $this->belongsTo('App\Employer','id_em');
}
}
结果如下:
如果我遗漏了什么,请列出,我仍然是 Laravel
的初学者谢谢
我认为您的主键不是 laravel 所需要的。例如:
在 employers
table 中,laravel 需要 employer_id
但您的主键是 id_em
。请将您的关系更新为以下并再次检查:
class Employer extends Model{
protected $primaryKey = 'id_em';
protected $table = 'employers';
protected $guarded = array();
public function contratClient(){
return $this->hasMany('App\ContratClient','foreign_key_in_this_table', 'idcontrat');
}
}
class ContratClient extends Model
{
protected $primaryKey = 'idcontrat';
protected $table = 'contratClients';
public function employer(){
return $this->belongsTo('App\Employer','foreign_key_in_this_table', 'id_em');
}
}
还要检查它是否是 contratClients
类型,您的意思是 contractClients
吗?
模型关系应该是:
class Employer extends Model{
protected $primaryKey = 'id_em';
protected $table = 'employers';
protected $guarded = array();
public function contratClient(){
return $this->hasMany("App\ContratClient","id_emp", "id_em");
}
}
class ContratClient extends Model
{
protected $primaryKey = 'idcontrat';
protected $table = 'contratClients';
public function employer(){
return $this->belongsTo('App\Employer','id_emp', 'id_em');
}
}
要验证关系,首先尝试获取行:
$data = Employer::with('contratClient')->get();