我有两个MSSQL表,所以我创建了两个模型[Adress]和[Webshop]。外键是两个表中的Adresse。如何检索Laravel 5.4中的一对一关系数据
1.型号[ADRESS]
class Adress extends Model
{
protected $table = "Adress";
protected $primaryKey = 'Adresse';
public $timestamps = false;
public function webshop()
{
return $this->hasOne('App\Webshop', 'Adresse');
}
}
2.型号[网上商店]
class Webshop extends Model
{
protected $table = "Webshop";
protected $primaryKey = 'Adresse';
public $timestamps = false;
public function adress()
{
return $this->belongsTo('App\Adress','Adresse');
}
}
我想作的表与从第一和第二表像webshopID一些数据,手机在[网上商店]表中,并在[地址]表中添加地址。我认为这是两张表格之间的一对一关系。
在php artisan tinker
:
App\Adress::all(); -> this is working
App\Adress::find(2910)->webshop -> this is also working
App\Adress::with('webshop')->get() -> this is NOT working
我想在同一时间检索从这个两个表的数据。这是可能与一个关系,或者我希望使用连接?
编辑: 也许我foreignKeys是错误的
您的模型都有'protected $ primaryKey ='Adresse';'?看起来有点不可思议,或者是“Adresse”你的默认ID列名?你得到了什么确切的错误? –
详细阐述了表格架构 –
其默认的id列名称。一个int作为主键。我没有得到任何错误,我等了2-3分钟,然后什么也没有。 –