【问题标题】:How to do this query with Laravel query builder by more mysql and php如何通过更多 mysql 和 php 使用 Laravel 查询生成器进行此查询
【发布时间】:2014-11-11 15:37:01
【问题描述】:

'正在尝试在 Laravel 中运行此查询,并希望使用查询和 php 构建器来执行此操作,因此我可以使用 paginate() 方法。但我似乎无法弄清楚如何让这个子查询运行。

    foreach ($array AS &$value)
    {
        if (strlen($value)==3)
        $l3[]="'".$value."'";
        else 
        $l4[]="'".$value."'";
        $valuex[]=$value;
    }
    if ($l4  && $l3)
    $where.="AND (left(field,3) IN (".join(", ",$l3).") || left(field,4) IN (".join(", ",$l4)."))";
    else if ($l3)
    $where.="AND (left(field,3) IN (".join(", ",$l3)."))";
    else 
    $where.="AND (left(field,4) IN (".join(", ",$l4)."))";
$sql=query("SELECT * FROM simso where field $where");

我想转换成 laravel 。

Simplemodel::where(..........

提前致谢!

【问题讨论】:

  • 您需要更好地指示查询试图实现的目标。我会首先考虑优化您的查询(它可能会尝试做的事情看起来非常复杂),然后研究如何将它应用到 Eloquent。

标签: laravel


【解决方案1】:

您可以使用Simplemodel::whereRaw($where)->get();

【讨论】:

    猜你喜欢
    • 2013-05-30
    • 2018-09-28
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 2020-12-25
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多