laravel - 在 Laravel 中加入两个表
问题描述
我对 Laravel 很陌生,在这里我有一个简单的问题。我需要再将一张表加入以下
return $products = Product::where('product_type_id', 1)
->where('quantity', '>=', 50)
->where('deleted', 0)
->orderBy('price')
->paginate(100);
我有另一个表名 product_img ,其中存储了所有产品图像。需要获取 product_img 表中产品 id 可用且文件不等于“Noimg.jpg”的所有记录。我尝试了不同的解决方案,但没有奏效。如何在 Laravel 中连接两个表?
我尝试执行以下操作:
$products = Product::leftJoin('product_img', function($join) {
$join->on('products.id', '=', 'product_img.product_id')
->where('product_img.file', '!=', 'Noimg.jpg');
}) ;
->where('quantity', '>=', 100)
->where('deleted', 0)
->orderBy('price')
->paginate(100);
解决方案
$products = Product::join('product_img', 'products.id', '=', 'product_img.product_id')
->where('product_img.file', '!=', 'Noimg.jpg')
->where('quantity', '>=', 100)
->where('deleted', 0)
->orderBy('price')
->paginate(100);
推荐阅读
- java - Java项目,Spring/显示学生问题/错误的数据库实现,SQL
- tensorflow.js - tfjs-models handpose 可以返回多个预测吗?
- git - 撤消推送并取消 PR 请求
- php - 什么是“掩盖”渐进式 ID 的好方法?
- odoo - 正确使用 t-attf-class 来禁用带有 Odoo 的标题按钮中的按钮
- javascript - 从文件夹中的文件生成并将 css 样式应用于 img 或容器(基于文件名)-(Javascript/JQuery/PHP?)
- java - 日期(MM/DD/YYYY HH:MM)大于 14:00 的 Apache POI 条件格式
- c++ - 使用带类的链表
- authentication - 如何在 Spring Security xml 配置中为登录页面添加内容安全策略标头?
- javascript - 如何使用lodash检查所有属性是否为空或空字符串?