0

-- Query I want to use :

select * from users where (email like '%[email protected]%' OR username like 'myusername') AND (password like '%1234@abcde%')

-- My laravel code for above query is :

$getInfo = Users::where('password','like','%'.$request->password.'%')
            ->where(function($query){
                $query->where('email','like','%'.$request->username.'%')
                ->orWhere('username','like','%'.$req->username.'%');
            })->get();

Here I am getting an error Undefined variable: request

3
  • You can use WhereRaw( ((email like '%'.$email.'%' OR username like '.$username.') AND (password like '%'.$password.'%')) ). Also its not my place to ask but why aren't you encrypting password while saving? Commented Jun 14, 2018 at 9:55
  • I am using laravel so I guess it would be good if I should use laravel functions rather than using raw query. Commented Jun 14, 2018 at 9:59
  • Its inbuild laravel function. At the end all the eloquent queries are changed by laravel to SQL commands for sql database. Commented Jun 14, 2018 at 10:01

2 Answers 2

2

You need to pass $request to your closure function like use ($request)

$getInfo = Users::where('password','like','%'.$request->password.'%')
            ->where(function($query) use ($request){
                $query->where('email','like','%'.$request->username.'%')
                ->orWhere('username','like','%'.$req->username.'%');
            })->get();

Also its not a good idea to search user details using like with wildcard (%) on both sides, it should be just like without wildcards or just use equality comparison =

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for the help. It works but it does not return any data.
It returns only [] with message "no properties" in my ajax response
Can you check you have exact data in DB against the data you receive from $request also do check the final sql generated by query builder using toSql() instead of get()
it returns select * from users where password = ? and (email = ? or username = ?)
@Moksh check binding values for these (?) placeholders and then evaluate your query
|
1
$getInfo = Users::where('password','like','%'.$request->password.'%')
            ->where(function($query) use ($request){
                $query->where('email','like','%'.$request->username.'%')
                ->orWhere('username','like','%'.$request->username.'%');
            })->get();

You have can variables to closure using use ($request).

You have also a typo

change

->orWhere('username','like','%'.$req->username.'%');

to

->orWhere('username','like','%'.$request->username.'%');

1 Comment

Thank you for your support and yes it was my typo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.