2017-04-13 91 views
1

我用这个包:https://github.com/jenssegers/laravel-mongodb如何在laravel中使用“with”?

我laravel雄辩是这样的:

$query = Product::where('store_id', $id)  
       ->with('store') 
       ->get(); 

我的产品型号是这样的:

public function store() 
{ 
    return $this->belongsTo(Store::class, 'store_id', '_id'); 
} 

当我执行dd($query),结果是这样的:

enter image description here

而我在数据库中看到,该数据存在

我尝试改变是这样的:

return $this->belongsTo(Store::class, 'store_id', 'id'); 

这是相同的

我怎样才能解决这个问题?

+1

请详细解释您的问题。 – Qazi

+0

您真的确定,您在商店表中拥有该特定ID的商店吗?这似乎更有可能破坏了您的数据库完整性 –

回答

0

public function store() 
{ 
    return $this->belongsTo(Store::class, null, 'store_id', '_id'); 
} 

尝试另外补充

protected $primaryKey = 'your_primary_key'; 

到您的模型。

希望这会有所帮助。 :)

0

我想你可以解决你的问题,以下这一点:

$query = Product::with('store')->where('store_id', $id)  
      ->get(); 

,并在型号:

public function store() 
{ 
    return $this->belongsTo('App\Store','store_id', '_id'); 
} 

并运行此命令:在命令提示符“作曲家转储自动加载”。

谢谢.....