【问题标题】:Laravel-5 'LIKE' equivalent (Eloquent)Laravel-5 'LIKE' 等效(雄辩)
【发布时间】:2015-08-26 00:52:33
【问题描述】:

我正在使用以下代码通过 Laravel 5 从数据库中提取一些结果。

BookingDates::where('email', Input::get('email'))->orWhere('name', 'like', Input::get('name'))->get()

但是,orWhereLike 似乎没有匹配任何结果。该代码在 MySQL 语句方面会产生什么?

我正在尝试实现以下目标:

select * from booking_dates where email='my@email.com' or name like '%John%'

【问题讨论】:

    标签: php mysql laravel-5


    【解决方案1】:

    如果您想查看数据库中运行的内容,请使用dd(DB::getQueryLog()) 查看运行了哪些查询。

    试试这个

    BookingDates::where('email', Input::get('email'))
        ->orWhere('name', 'like', '%' . Input::get('name') . '%')->get();
    

    【讨论】:

    • 这个查询sql注入是否受到保护?
    • @partho 是的。 Laravel 筛选您作为 where 方法的第三个参数传递的整个字符串。
    • 当注入受到保护时,您可能需要检查用户输入中的意外百分比。例如,LIKE "%John%" 和 LIKE "John%" 的表现不同(您可能只打算使用后者)。还要考虑空输入,然后单独考虑“%”,这也可能导致上述代码的意外结果。
    • 同意伊恩。 Laravel 只进行部分转义。如果你没有正确地逃避 LIKE,仍然有很多恶作剧可能。方法如下:stackoverflow.com/a/42028380/329062
    • 我添加了preg_replace("/[^A-Za-z0-9 ]/", '', $search);,因为我不需要特殊字符,它也可以保护它免受% 注入
    【解决方案2】:

    我有这个范围,希望它对某人有所帮助。 https://laravel.com/docs/master/eloquent#local-scopes

    public function scopeWhereLike($query, $column, $value)
    {
        return $query->where($column, 'like', '%'.$value.'%');
    }
    
    public function scopeOrWhereLike($query, $column, $value)
    {
        return $query->orWhere($column, 'like', '%'.$value.'%');
    }
    

    用法:

    $result = BookingDates::whereLike('email', $email)->orWhereLike('name', $name)->get();
    

    【讨论】:

      【解决方案3】:
      $data = DB::table('borrowers')
              ->join('loans', 'borrowers.id', '=', 'loans.borrower_id')
              ->select('borrowers.*', 'loans.*')   
              ->where('loan_officers', 'like', '%' . $officerId . '%')
              ->where('loans.maturity_date', '<', date("Y-m-d"))
              ->get();
      

      【讨论】:

      • ->where('loan_officers', 'like', '%' . $officerId . '%') 其中loan_officers 是序列化字段
      【解决方案4】:

      我认为这更好,遵循将参数传递给查询的良好做法:

      BookingDates::whereRaw('email = ? or name like ?', [$request->email,"%{$request->name}%"])->get();
      

      You can see it in the documentation, Laravel 5.5.

      您还可以使用 Laravel scout 并使其更容易进行搜索。 Here is the documentation.

      【讨论】:

        【解决方案5】:

        下面提到的查询对我有用,也许对某人有帮助。

         $platform = DB::table('idgbPlatforms')->where('name', 'LIKE',"%{$requestedplatform}%")->first();
        

        【讨论】:

          【解决方案6】:

          如果您想在控制器上使用它,您可以执行以下操作:

          $generatequery = 'select * from blogs where is_active = 1 and blog_name like '%'.$blogs.'%' order by updated_at desc, id desc';

          $blogslists = DB::select($generatequery);

          【讨论】:

            【解决方案7】:

            如果您使用 Postgres,可以使用关键字 ILIKE 代替 LIKE 以根据活动区域设置匹配不区分大小写。这不在 SQL 标准中,而是 PostgreSQL 扩展。

            这对我有用。

            User::where('name, 'ILIKE', $search)->get();
            

            postgres documentation

            【讨论】:

            • 我没有得到What does that code produce in terms of [SQL] statements? 和以上回复之间的联系 - 这个回答什么问题?
            • 那么使用这个不安全
            猜你喜欢
            • 2015-05-22
            • 2021-09-29
            • 2019-01-28
            • 2015-08-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多