【发布时间】:2019-08-17 20:42:02
【问题描述】:
我有 3 个安装了外键的表。
customers {customer_id, customer_name}
products {product_id, product_name}
customer_products {id, customer_id (foreignkey), product_id (foreignkey)}
我的控制器代码:
$CustomerProducts = ModelName::where('customer_id', 'somevalue')
->Join('customer_products', 'product_id', '=', 'customer_id')
->get();
我的型号代码:
class ModelName extends Model {
protected $table = 'hd_products';
public $primaryKey = 'id'; }
我的代码有什么问题,因为我得到了错误的结果。我想展示客户信息及其相关产品。
【问题讨论】:
-
@Watercayman 正确指出,这是 laravel 中的
Many to Many关系,您可以在此处阅读更多信息:laravel.com/docs/5.8/eloquent-relationships#many-to-many
标签: php mysql laravel laravel-5 eloquent