【问题标题】:how to use exact pivot table relationship如何使用精确的数据透视表关系
【发布时间】:2019-05-17 21:10:21
【问题描述】:

我在数据库(sql)中有四个表:

客户表:

id
name

学分表:

id
name
count

customer_credit 表(数据透视表):

customer_id
credit_id
expiry_date

消费者表:

id
customer_id

这是带有 laravel 的 SPA 应用程序(Web 库)。 如何从消费者表中接收(仅登录客户)记录,在 customer_credit 表中有条件的可用记录没有过期日期,我们可以计算剩余积分。更多解释是我们正在尝试从我们的游泳池和健身房的客人那里收集入口和出口日志,我们希望在 credits 表中获得确切的 customer_credit 表 id 的计数。

【问题讨论】:

标签: php laravel


【解决方案1】:

第一个选项,使用Laravel Query Builder

DB::table('consumers')
     ->select('consumers.*')
     ->join('customers', 'consumers.customer_id', '=', 'customers.id')
     ->join('customer_credit', 'customers.id', '=', 'customer_credit.customer_id')
     ->join('credits', 'customer_credit.credit_id', '=', 'credits.id')
     ->where('credits.count', '>', 0)
     ->where('customer_credit.expiry_date', '>=', date('Y-m-d'))
     ->get();

第二个选项,使用Laravel Eloquent。如果您选择第二个并需要帮助,您应该在此处分享您的模型结构。

另请阅读此主题以查看差异:Laravel Eloquent vs query builder - Why use eloquent to decrease performance

【讨论】:

    猜你喜欢
    • 2014-06-13
    • 2017-07-22
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 2014-08-26
    • 2021-01-19
    相关资源
    最近更新 更多