【发布时间】:2013-09-20 09:43:10
【问题描述】:
代码:
<?php
class Catering extends \Eloquent {
protected $table = 'catering';
public $timestamps = FALSE;
public function offers() {
return $this->hasMany('Offer', 'cid');
}
}
class Offer extends \Eloquent {
protected $table = 'catering_offer';
public $timestamps = FALSE;
public function catering() {
return $this->belongsTo('Catering');
}
}
我能做到
$offers = Catering::find(1)->offers;
但是,反之不行:
$catering = Offer::find(1)->catering;
总是返回 NULL。数据库具有正确的值。
报价表有 2 列:
primary(id), int(cid)
引用了 catering.id。
问题: 我如何访问这种关系的反面?
【问题讨论】:
-
没有看到正在生成的 SQL,我不能肯定地说。但是您是否尝试过使用
$this->belongsTo('Catering', 'cid');,因为我认为您的用途是在offer表中查找列catering_id。 -
@carbontwelve 我认为你是对的
标签: php laravel laravel-4 eloquent