【发布时间】:2014-03-07 02:22:24
【问题描述】:
我尝试从实体中获取城市名称。我得到这样的实体:
$entity = Entity::find(1);
我想得到这样的城市名称:
$entity->addresses->cities->name
但它不起作用?我尝试了多种方法,但无法获得实体对应的城市。
这是我的模型:
实体:
class Entity extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'entities';
public function addresses ()
{
return $this->hasMany('Address');
}
地址:
class Address extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'addresses';
public $timestamps = false;
public function cities ()
{
return $this->belongsTo('City');
}
public function entities ()
{
return $this->belongsTo('Entity');
}
城市:
class City extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'cities';
public function addresses ()
{
return $this->hasMany('City');
}
我必须进行中间操作吗?还是我必须在我的模型中添加一些东西?
我有这个错误:
未定义属性:Illuminate\Database\Eloquent\Collection::$cities(查看:/Applications/MAMP/htdocs/project/app/views/administrator/general.blade.php)
感谢您的帮助
【问题讨论】:
标签: php laravel laravel-4 eloquent