【问题标题】:Returning all collections relational table返回所有集合关系表
【发布时间】:2017-04-13 01:06:01
【问题描述】:

我正在重试与特定对象相关的所有数据,但不是只获取与产品相关的数据,而是从表中获取所有数据。

控制器:

$product = Product::find($id);
return $product->with('detailsProduct')->get();

模型产品:

public function detailsProduct(){
        return $this->hasMany(ProductTranslation::class);
    }

【问题讨论】:

  • Product::with('detailsProduct')->find($id) 是否返回相同的结果?如果是这样,这 2 个表中的列是什么?

标签: laravel eloquent laravel-5.2 laravel-5.3


【解决方案1】:

你可以试试:

$product = Product::with('detailsProduct')->where('id', $id)->first();
return $product;

【讨论】:

  • 你也可以$product = Product::with('detailsProduct')->find($id);。但是,虽然这确实解决了 OP 的问题,但请添加解释为什么它解决了问题。没有解释,没有人会从这个答案中学到东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-22
  • 2018-02-12
  • 1970-01-01
  • 1970-01-01
  • 2012-01-16
  • 2020-09-08
  • 2020-06-03
相关资源
最近更新 更多