php - 如何在 laravel eloquent 中正确使用或在哪里?

标签 php mysql laravel-5

我想在 laravel elequent 中执行以下查询

select from table where cond1 and(cond or cond or cond..)and cond3.

我使用了以下代码,

$invoice = Invoice::with("items", "salesPerson", "client");
if ($q = Request::input("q")) {
        $invoice->where("invoices.invoice_number", 'LIKE', "%$q%");
        $invoice->orWhere("invoices.reference_number", 'LIKE', "%$q%");
        $invoice->orWhere("invoices.client_name", 'LIKE', "%$q%");
        $invoice->orWhere("invoices.invoice_date", 'LIKE', "$q%");
        $invoice->orWhere("invoices.due_date", 'LIKE', "$q%");
    }
$invoice->whereNull("invoices.deleted_at");

但它也会返回已删除的记录。我该如何解决这个问题?

最佳答案

在本例中为 nested where派上用场:

$invoice = Invoice::with("items", "salesPerson", "client");
if ($q = Request::input("q")) {
    $invoice->where(function($query) use ($q){
        $query->where("invoices.invoice_number", 'LIKE', "%$q%");
        $query->orWhere("invoices.reference_number", 'LIKE', "%$q%");
        $query->orWhere("invoices.client_name", 'LIKE', "%$q%");
        $query->orWhere("invoices.invoice_date", 'LIKE', "$q%");
        $query->orWhere("invoices.due_date", 'LIKE', "$q%");
    });
}
$invoice->whereNull("invoices.deleted_at");

关于php - 如何在 laravel eloquent 中正确使用或在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30703140/

相关文章:

Laravel 5 打包计划任务

laravel-5 - 在 Laravel Scheduler 函数中, everyMinute() 不起作用

php - RTL 格式数字和符号未正确对齐

php - cakephp数据编辑和更新时如何从网站根目录中删除图像

php - 在 WAMP 上安装 IMAGEMAGICK 的分步说明?

php - 如何使用php从mysql获取最近24小时的记录

MySQL:获取所有过程输出参数为空

php - 是否可以通过这种方式破解使用未经过滤的 GET 参数的网站

mysql - "ON DELETE SET NULL"mysql mamp 错误

php - Laravel 5.2 - srmklive/laravel-paypal 问题