我试图将同一表中的2列相乘,然后直接在laravel中的where语句中使用结果 [英] I tried to multiply 2 columns in same table and directly use the result in where statement in laravel

查看:203
本文介绍了我试图将同一表中的2列相乘,然后直接在laravel中的where语句中使用结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

$contributions = 
  auth()->user()->cooperative->contributions->where("amount", "<", 'amount * shares');

但这不会返回所需的结果.如何将数量和份额相乘,它们来自同一张表并使用其乘积.

But this doesn't return required results. How can I multiply amount and shares, they are from same table and use their products.

推荐答案

操作时:

...->contributions->where("amount", "<", 'amount * shares');

Laravel将不知道amountshares是同一表的列.它将完全将amount * shares视为字符串.

Laravel will not know that amount and shares are columns of the same table. It will treat amount * shares as a string entirely.

要让laravel将其视为MySQL语法,可以使用whereRaw:

To let laravel consider this as a MySQL syntax, you can use whereRaw :

...->contributions->whereRaw('amount < amount * shares');

另一种方法是使用whereColumn:

...->contributions->whereColumn('amount', '<', \DB::raw('amount * shares'));

希望这会有所帮助.有关更多信息,请参见文档

Hope this helps. For more check documentation

这篇关于我试图将同一表中的2列相乘,然后直接在laravel中的where语句中使用结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆