php - Eloquent Query 用于获取用户没有完成交易的所有用户失败交易

标签 php mysql laravel eloquent

我很难构建一个 Eloquent 查询来提供一个问题案例交易的仪表板。问题定义为用户未完成交易的所有失败交易。

我有两个表,userstransactions。一个用户可以有很多交易,一个交易的状态可以是已完成失败

我需要一个 Eloquent 查询来从所有至少有一个失败交易并且没有任何完成交易的用户那里获取失败交易列表。我目前通过查询用户而不是交易从另一个方向来解决这个问题

$failed_transactions = User::has('transactions')->whereDoesntHave('transactions', function($query) {
     $query->where('status', 'completed');
})->get();

问题在于它返回的是用户列表而不是失败的交易。

如有任何帮助,我们将不胜感激!

最佳答案

I need a single eloquent query to get a list of failed transactions from all users who have at least one failed transaction, and have not had any completed transactions.

如果你想获得一个失败的交易集合:

Transaction::whereHas('user.transactions', function($q) {
        $q->where('status', 'failed');
    })
    ->whereDoesntHave('user.transactions', function($q) {
        $q->where('status', 'completed');
    })
    ->where('status', 'failed')
    ->get()

如果您想让用户处理失败的交易:

User::whereHas('transactions', function($q) {
        $q->where('status', 'failed');
    })
    ->whereDoesntHave('transactions', function($q) {
        $q->where('status', 'completed');
    })
    ->with(['transactions' => function($q) {
        $q->where('status', 'failed');
    }])
    ->get()

关于php - Eloquent Query 用于获取用户没有完成交易的所有用户失败交易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48114307/

相关文章:

php - 用于在移动设备中显示视频的 Html 页面

php - 从一个数组中的sql中获取数据

mysql - 一行/水平显示MySQL查询的输出

php - 如何使我的文件上传后端脚本(PHP、MySQL)线程安全?

php - Laravel 自定义验证——获取参数

javascript确认向get/post添加参数

javascript - 具有相同名称和不同 id 的选择字段数组。

mysql - MAMP Pro 和 MySQL 出现 fatal error : Please read "Security" section of the manual to find out how to run mysqld as root

php - 在 Laravel 中创建搜索工具

php - 现实世界场景中的 Laravel PayPal 教程