user3606724
user3606724

Reputation:

Using 'LIKE' in laravel

Hello I am having trouble getting values in my laravel code. See here and notice ->where('created_at', 'LIKE', '%'.'2017-04-12'.'%')

$filtered_table = Method::leftJoin('users', 'users.id', '=', 'methods.created_by')
      ->leftJoin('roles', 'roles.id', '=', 'users.role_id')
      ->leftJoin('types', 'types.id', '=', 'methods.type_id')
      //->where($request->filters)//will act as a searchmap
      ->where('created_at', 'LIKE', '%'.'2017-04-12'.'%')
      ->get([ 'users.username', 'users.id AS users_id', 'methods.*', 'methods.id AS method_id', 'methods.name AS method_name', 'roles.id AS role_id', 'roles.name AS role_name',
      'types.id AS type_id_typetable', 'types.name AS type_name']);

My question is what's the proper way of using 'LIKE' because I can't get any values from it. The reason I am using it is that because that column has other string to it. See this picture.

enter image description here enter image description here

Is it possible to only read/or find '2017-04-12' in that column(I am doing this for testing purposes).

Upvotes: 1

Views: 59

Answers (2)

Sagar Arora
Sagar Arora

Reputation: 1773

You can use it like:

Method1 using Raw query:

$q->where(DB::raw("DATE(created_at) = '".date('Y-m-d')."'"));

Method 2 by whereDate:

$q->whereDate('created_at', '=', date('Y-m-d'));

For more details visit:

http://laraveldaily.com/eloquent-date-filtering-wheredate-and-other-methods/

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

Since you're using Eloquent, I'd recommend you to use whereDate() instead:

$date = Carbon::parse('2017-04-12')->toDateString();
....
->whereDate('created_at', $date)

If you don't have Carbon object, you can just use string:

->whereDate('created_at', '2017-04-12')

Upvotes: 1

Related Questions