【问题标题】:Eloquent relationships laravel雄辩的关系 laravel
【发布时间】:2015-01-11 06:38:25
【问题描述】:

我有 3 张桌子

类别

  • 身份证
  • 姓名

categories_product

  • 身份证
  • category_id
  • product_id

还有一个产品表

产品

  • 身份证
  • 姓名

我想要基于 catargori_id 的产品,我如何使用 laravel Eloquent 做到这一点?

【问题讨论】:

  • 你已经创建模型了吗?
  • 是的,但我不知道如何使用 category_id 从产品表中查询数据。

标签: laravel-4 eloquent database-relations


【解决方案1】:

就这么简单:

Product::whereCategori_id($category)->get();

【讨论】:

  • 感谢您的回答。你的意思是这样吗?产品::where('Categori_id', '=', $category_id)->get();那我应该使用哪种关系?一对多?
  • 也是如此。作为一个产品属于一个类别,你应该使用belongsTo关系:)
  • $products = DB::table('products') ->leftJoin('category_products', 'products.id', '=', 'category_products.product_id') ->where('category_products .category_id', '=', '1')->get();工作:)
  • 您的解决方案出现此错误。 SQLSTATE [42S22]:未找到列:1054 'where 子句'中的未知列 'category_id'(SQL:select * from products where category_id = 1)
猜你喜欢
  • 2015-03-14
  • 2018-11-28
  • 2021-06-03
  • 2020-03-26
  • 1970-01-01
相关资源
最近更新 更多