PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, February 17, 2022

[FIXED] How to query from two columns with one string in Laravel

 February 17, 2022     laravel, laravel-5, laravel-6     No comments   

Issue

I have two columns in the users table: first_name & last_name. I want to query the user search john doe. I attempted it like the following.

$users = User::where('username', $request->text)
    ->orWhere('first_name', 'LIKE', '%'.$request->text.'%')
    ->paginate(5);

It searches only through john and I don't understand how to make it work


Solution

Try the following...

$users = User::where('username', $request->text)
    ->orWhereRaw("CONCAT(first_name, ' ', last_name) 
        LIKE ?", ['%' . $request->text . '%'])
    ->paginate(5);

The whereRaw and orWhereRaw methods can be used to inject a raw where clause into your query.



Answered By - Karl Hill
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing