【问题标题】:Laravel Eloquent ORM seemed to be easyLaravel Eloquent ORM 似乎很简单
【发布时间】: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


【解决方案1】:

我的坏!

Ma 数据库播种机保存种子 140 个客户和 35 个联系人。 我尝试获得第 140 个联系人...

我的代码完美运行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-08
    • 2023-02-26
    • 2013-02-12
    • 2016-07-01
    • 2016-09-24
    • 2013-12-28
    • 2019-08-01
    • 1970-01-01
    相关资源
    最近更新 更多