gpt4 book ai didi

php - 如何在 laravel 中使用 "with"?

转载 作者:可可西里 更新时间:2023-11-01 10:41:03 26 4
gpt4 key购买 nike

我使用这个包:https://github.com/jenssegers/laravel-mongodb

我的laravel Eloquent 是这样的:

$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');

是一样的

我该如何解决这个问题?

最佳答案

我认为您可以按照以下方式解决您的问题:

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

在模型中:

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

然后在命令提示符下运行此命令:'composer dump-autoload'。

谢谢.....

关于php - 如何在 laravel 中使用 "with"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43382999/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com