【发布时间】:2014-08-28 21:45:17
【问题描述】:
我尝试用 ORM 连接表。 我有一个“客户”表和“联系人”。
在我的模型中我写了这个:
class Customer extends Eloquent {
protected $fillable = [];
protected $softDelete = true;
protected $guarded = array();
public function contacts()
{
return $this->hasMany('Contact');
}
}
还有这个
class Contact extends Eloquent {
protected $fillable = [];
protected $softDelete = true;
protected $guarded = array();
public function customer()
{
return $this->belongsTo('Customer');
}
}
当我尝试时在我的控制器中
return Customer::find($id)->contacts
我没有结果:/
我错过了什么?
谢谢
【问题讨论】:
-
您的代码看起来不错,可能是数据本身有问题?因此,也许在 DB 中没有正确链接该客户的联系人,或者可能是软删除的记录。
-
数据库联系人 = |客户 ID |姓名 | ... 数据库客户 = |姓名 |电话 | ...(没有contact_id)您认为软删除会产生问题?
-
但是数据呢?实际上是否有具有指定客户 ID 的联系人记录。他们可能被软删除了吗?尝试运行
Customer::find($id)->contacts()->withTrashed()->get() -
我觉得我的数据库没问题? img849.imageshack.us/img849/2463/hp12.png
-
Customer::find($id)->contacts()->withTrashed()->get() 没有结果。我要疯了! ^^
标签: php database laravel orm eloquent