2017-04-13 87 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'); 
} 

並運行此命令:在命令提示符「作曲家轉儲自動加載」。

謝謝.....