search

Home  >  Q&A  >  body text

How to write the fuzzy search syntax of Eloquent ORM in Laravel5.2?

How to write the fuzzy search syntax of Eloquent ORM in Laravel5.2?

巴扎黑巴扎黑2857 days ago549

reply all(2)I'll reply

  • 迷茫

    迷茫2017-05-16 16:56:23

    It seems that you can use the where statement.

    $users = DB::table('users')->where('name', 'like', '%hello%')->get();
    

    If you need a writing method unique to a certain database, or are not used to Eloquent's method, you can also use native SQL statements (not recommended, especially since it is easy to cause SQL injection vulnerabilities due to spelling strings) - use DB: :raw method.

    $users = DB::table('users')
                         ->select(DB::raw('count(*) as user_count, status'))
                         ->where('status', '<>', 1)
                         ->groupBy('status')
                         ->get();

    I just moved it from the documentation. . .

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-16 16:56:23

    I recommend you an elegant writing method, which is similar to TP. Please look here
    Then you can write $map = ['aa' => ['LIKE' => '%XXX%' ];

    reply
    0
  • Cancelreply