[英]Relations not working in Laravel and MongoDB?
你好iam工作LAravel和MOngodb App:我有以下场景:我有评论和他们的点击集合如下:Hoots Model Collection
"_id": ObjectId("52ef89e9c6b93ca123f2fd37"),
"user_id": "52df9ab5c6b93c8e2a8b4567",
"hoot": "Hello Thius is Tewst",
"updated_at": ISODate("2014-02-03T12:22:01.530Z"),
"created_at": ISODate("2014-02-03T12:22:01.530Z")
和它的点击模型收集像这样:
"_id": ObjectId("52ef8d5ec6b93c6d24f2fd37"),
"created_at": ISODate("2014-02-03T12:36:46.130Z"),
"dip": NumberInt(1),
"hits": NumberInt(0),
"post_id": "52ef89e9c6b93ca123f2fd37",
"updated_at": ISODate("2014-02-03T13:35:20.766Z")
所以这里有一个评论,即hoot有一个集合描述他们的喜欢和不喜欢
我有一个模特anem Hoots
<?php
use Jenssegers\Mongodb\Model as Eloquent;
class Hoots extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'hoots';
public $timestamps = true;
protected $fillable = array('user_id', 'hoot');
public function gethit()
{
return $this->hasOne('Hitdip');
}
}
和Hitdip模型为他们的命中记录
<?php
use Jenssegers\Mongodb\Model as Eloquent;
class Hitdip extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'hitdip';
public $timestamps = true;
protected $fillable = array('post_id','hits', 'dip' ,'type');
public function gethoot()
{
return $this->BelongsTo('Hoots');
}
}
但是,当我试图获取hoot并记录与他们相关的记录时,我收到了一个错误
Hoots::where('user_id',$user_id)->gethit;
请回顾一下? 错误
Undefined property: Jenssegers\Mongodb\Eloquent\Builder::$gethit (View: /var/www/hututoo/app/views/profile/profile_connect/profile.blade.php) (View: /var/www/hututoo/app/views/profile/profile_connect/profile.blade.php)
在一对一的关系中,事情很简单。 以上关系定义正确。 但我们需要以这种格式获取东西
Hoots::with('gethit')->find(id)->get();
Hoots::with('gethit')->where('user_id',$user_id)->get();
有些时候Class name不会被视为对象,所以你必须直接传递给函数。
您需要更改gethit和gethoot函数,并且需要传递类对象示例:
public function gethit(){
return $this->hasOne(new gethit(),'Hoots','post_id','_id');
}
与gethoot相同
public function gethoot(){
return $this->BelongsTo(new Hoots());
}
像Hoots这样的一对多关系有很多帖子。 所以你可以打电话
$posts = Hoots::with('gethit')->where('user_id','52df9ab5c6b93c8e2a8b4567')->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.