Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wildcard-like syntax in an eloquent Where clause?

Here is a where clause I have:

->where( 'post_type', '=', 'blog' )

Is there any way to replace 'blog' by a wildcard, so it will match any 'post_type' ?

Full query:

$db = ( new DbSql() )->db()->getConnection();
$postsCount = $db->table( 'posts' )
                          ->where( 'status', '=', 'published' )
                          ->where( 'post_type', '=', 'blog' )
                          ->where( 'alerts', '<', Settings::CONTENT_ALERT_NUMBER_ALLOWANCE )
                         ->count() ?? null;
like image 534
Robert Brax Avatar asked Apr 19 '17 09:04

Robert Brax


People also ask

How do you use like in eloquent?

Using Eloquent you can perform a search like this: User::query() ->where('name', 'LIKE', "%{$searchTerm}%") ->orWhere('email', 'LIKE', "%{$searchTerm}%") ->get();

Where like in laravel query?

You can use the LIKE MySQL keyword and % wildcard character with where clause. In laravel, using whereLike() eloquent method, you can implement laravel where like search query, laravel where like multiple columns and laravel collection with where like.

Is eloquent an ORM?

Eloquent is an object relational mapper (ORM) that is included by default within the Laravel framework. An ORM is software that facilitates handling database records by representing data as objects, working as a layer of abstraction on top of the database engine used to store an application's data.


2 Answers

Use like:

->where('post_type', 'like', '%'.$string.'%')
like image 126
Alexey Mezenin Avatar answered Oct 06 '22 10:10

Alexey Mezenin


Try this

$query->where($field, 'like', $input . '%');
like image 3
Harsha W Avatar answered Oct 06 '22 09:10

Harsha W