Laravel 7 查询 with() 并使用 Where()
Laravel 7 Query with() and using Where()
大家好,我有一个看起来像这样的查询
$query = Transaction::with(['customer', 'merchant', 'batch'])
->select(sprintf('%s.*', (new Transaction)->table));
我需要根据属于当前登录用户的 iso_id 过滤交易。
$query = Transaction::with(['customer', 'merchant', 'batch'])
->select(sprintf('%s.*', (new Transaction)->table))
->where('merchant.iso_id', '=', auth()->user()->isIso());
我要比较的iso_id,在商家里面table
auth()->user()->isIso() returns 如果为真则正确 iso_id 否则发送假
所以我第一次尝试使用 where('merchant.iso_id', '=', auth()->user()->isIso())
但是 returns 该列不存在,因为出于某种原因,它没有从交易模型切换到商家模型。
我不确定如何使用 with() 中的内容作为 where() 的选择器
如有任何帮助,我们将不胜感激!
尝试使用 whereHas
添加约束:
$query = Transaction::with(['customer', 'batch'])
->whereHas('merchant', function ($q) {
$q->where('iso_id', auth()->user()->isIso());
})
->select(sprintf('%s.*', (new Transaction)->table))
->get();
大家好,我有一个看起来像这样的查询
$query = Transaction::with(['customer', 'merchant', 'batch'])
->select(sprintf('%s.*', (new Transaction)->table));
我需要根据属于当前登录用户的 iso_id 过滤交易。
$query = Transaction::with(['customer', 'merchant', 'batch'])
->select(sprintf('%s.*', (new Transaction)->table))
->where('merchant.iso_id', '=', auth()->user()->isIso());
我要比较的iso_id,在商家里面table
auth()->user()->isIso() returns 如果为真则正确 iso_id 否则发送假
所以我第一次尝试使用 where('merchant.iso_id', '=', auth()->user()->isIso())
但是 returns 该列不存在,因为出于某种原因,它没有从交易模型切换到商家模型。
我不确定如何使用 with() 中的内容作为 where() 的选择器
如有任何帮助,我们将不胜感激!
尝试使用 whereHas
添加约束:
$query = Transaction::with(['customer', 'batch'])
->whereHas('merchant', function ($q) {
$q->where('iso_id', auth()->user()->isIso());
})
->select(sprintf('%s.*', (new Transaction)->table))
->get();