Laravel Eloquent model query - php

I Want To Achieve the right query in laravel if anyone can help
I have This query
SELECT * FROM `users` WHERE ((`is_verified` = 1) AND (`first_name` like '%%' or `middle_name` like '%%' or `last_name` like '%%' or `email` like '%%'))
and i have this in my code
$user->where([
['is_verified', '=', 1]
])
->where('first_name', 'like', "%$search%")
->orWhere('middle_name', 'like', "%$search%")
->orWhere('last_name', 'like', "%$search%")
->orWhere('email', 'like', "%$search%");
but it produces
SELECT * FROM `users` WHERE ((`is_verified` = '1') and `first_name` like '%%' or `middle_name` like '%%' or `last_name` like '%%' or `email` like '%%')

You have to use an anonymous function in a "closure-where" clause.
$user->where([
['is_verified', '=', 1]
])->where(function ($query) use ($search) {
$query->where('first_name', 'like', "%$search%")
->orWhere('middle_name', 'like', "%$search%")
->orWhere('last_name', 'like', "%$search%")
->orWhere('email', 'like', "%$search%");
});

you must use anonymous function like following code:
User::where(function ($query){
$query->where('is_verified', 1);
})->where(function ($query) use ($search) {
$query->where('first_name', 'like', "%$search%")
->orWhere('middle_name', 'like', "%$search%")
->orWhere('middle_name', 'like', "%$search%")
->orWhere('email', 'like', "%$search%");
})->toSql();

Related

How to write this query in laravel? Any Suggestions?

This is the Query which is working fine in MY SQL:
SELECT users.name FROM ((event_user INNER JOIN events ON
event_user.event_id = events.id) INNER JOIN users ON
event_user.user_id = users.id) where events.name like '%FEB%' or
users.name like '%FEB%' or users.email like '%FEB%' or users.phone
like '%FEB%'
This is what I've written which is of course not working:
$data['search_data'] = EventUser::select('users.*')
->join
('users', 'event_user.user_id', '=', 'users.id')
->join
('events', 'event_user.event_id', '=', 'events.id')
->where
('events.name','like', "'$search'")
->orWhere
("users.id",'like', "'$search'")
->orWhere
("users.name",'like', "'$search'")
->orWhere
("users.phone",'like', "'$search'")
->orWhere
("users.email",'like', "'$search'")
->orWhere
("users.password",'like', "'$search'")
->orWhere
("users.work_location",'like', "'$search'")
->orWhere
("users.profession",'like', "'$search'")
->orWhere
("users.designation",'like', "'$search'")
->orWhere
("users.committee",'like', "'$search'")
->orWhere
("users.role",'like', "'$search'")
->orWhere
("users.bio",'like', "'$search'")
->paginate(10);
ok so, with the help from everyone.
I've got solution. Thank you all.
$data['search_data'] = EventUser::select('users.*', 'events.name as e_name')
->join('users', 'event_user.user_id', '=', 'users.id')
->join('events', 'event_user.event_id', '=', 'events.id')
->where('events.name', 'like', '%'.$search.'%')
->orWhere('users.id', 'like', '%'.$search.'%')
->orWhere('users.name', 'like', '%'.$search.'%')
->orWhere('users.phone', 'like', '%'.$search.'%')
->orWhere('users.email', 'like', '%'.$search.'%')
->orWhere('users.password', 'like', '%'.$search.'%')
->orWhere('users.work_location', 'like', '%'.$search.'%')
->orWhere('users.profession', 'like', '%'.$search.'%')
->orWhere('users.designation', 'like', '%'.$search.'%')
->orWhere('users.committee', 'like', '%'.$search.'%')
->orWhere('users.role', 'like', '%'.$search.'%')
->orWhere('users.bio', 'like', '%'.$search.'%')
->distinct()
->paginate(10);
But this is still returning the repetitive results.
You need to concat $search with % like this:
->where('events.name','like', '%'.$search.'%')
hmm, thats a long query, I think you can test this:
$data['search_data'] = EventUser::select('users.*')
->join('users', 'event_user.user_id', '=', 'users.id')
->join('events', 'event_user.event_id', '=', 'events.id')
->where('events.name', 'like', '%'.$search.'%')
->where(function ($query) use ($search) {
$query->where('title', 'LIKE', '%'.$search.'%')
->orWhere('users.id', 'LIKE', '%'.$search.'%')
->orWhere('users.name', 'LIKE', '%'.$search.'%')
->orWhere('users.phone', 'LIKE', '%'.$search.'%')
->orWhere('users.email', 'LIKE', '%'.$search.'%')
->orWhere('users.password', 'LIKE', '%'.$search.'%')
->orWhere('users.work_location', 'LIKE', '%'.$search.'%')
->orWhere('users.profession', 'LIKE', '%'.$search.'%')
->orWhere('users.designation', 'LIKE', '%'.$search.'%')
->orWhere('users.committee', 'LIKE', '%'.$search.'%')
->orWhere('users.role', 'LIKE', '%'.$search.'%')
->orWhere('users.bio', 'LIKE', '%'.$search.'%')
})
->paginate(10);
Hope it help. Let us know if it worked!

MySQL query to Laravel 5.5

SELECT * FROM table WHERE age = 10 AND (first_name like %name% OR last_name like %name%);
How do you do this in Laravel 5.5?
Using query builder:
$results = DB::table('table')
->where('age', 10)
->where(function ($query) {
$query
->where('first_name', 'like', '%name%')
->orWhere('last_name', 'like', '%name%');
})
->get();
Check the official documentation (5.5) for Query Builder
You can do something like this to query
$data= DB::table('table')
->whereRaw('age'= ? AND (first_name like ? OR last_name like ?)', [10,'%'.name.'%','%'.name.'%' ])
->get();'
Official documentation to select https://laravel.com/docs/5.6/queries#selects
I don't know how many field do you use in your view.. but if you have firstname field and lastname field, then your code should be like this:
$results = DB::table('table')
->where('age', 10)
->where(function ($query) use ($request) {
$query
->where('first_name', 'LIKE', '%' . $request->first_name .'%')
->orWhere('last_name', 'LIKE', '%' . $request->last_name .'%');
})
->get();

Eloquent grouped orWhere

Currently have an Eloquent statement:
$contacts = Contacts::where('lname','LIKE',$searchquery.'%')
->orWhere('fname','LIKE',$searchquery.'%')
->orWhere('phone','LIKE','%'.$searchquery)
->where('active','=',1)->get();
It is treating it as
select
*
from
contacts
where
lname like $searchquery+'%'
or lname like $searchquery+'%'
or lname like $searchquery+'%'
and active = 1
what I am needing is
select
*
from
contacts
where
(lname like $searchquery+'%'
or lname like $searchquery+'%'
or lname like $searchquery+'%')
and active = 1
How do I go about grouping in Eloquent? I have found a couple examples such as:
DB::table('users')
->where('name', '=', 'John')
->orWhere(function ($query) {
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
But I am only used to Eloquent, not Laravels DB Query builder. I tried adapting the Eloquent form to this
$contacts = Contacts::->where('active', '=', '1')
->where(function ($query) {
$query->orWhere('lname', 'LIKE', $searchquery.'%')
->orWhere('lname', 'LIKE', $searchquery.'%')
->orWhere('phone', 'LIKE', '%'.$searchquery);
})
->get();
No success as it does not recognize the $searchquery inside the function.
What am I missing?
So, this is what you have to do:
DB::table('users')->where(function($query) use ($searchQuery){
$query->where('lname', 'LIKE', $searchQuery . '%')
->orWhere('fname', 'LIKE', $searchQuery . '%')
->orWhere('phone','LIKE', '%' . $searchquery);
})
->get();
Note that I've put use ($searchQuery) so it can be used inside the closure
When you use orWhere with multiple where clauses, you need to be careful!
Where clauses after orWhere doesn't effected to sibling where clauses before orWhere clauses such as where, whereHas, whereDoesntHave, orWhereHas
also you have to pass $searchquery variable inside to function in where clause using use ($searchquery)
Contacts::->where(function ($query) use ($searchquery) {
$query->orWhere('lname', 'LIKE', $searchquery.'%')
->orWhere('lname', 'LIKE', $searchquery.'%')
->orWhere('phone', 'LIKE', '%'.$searchquery);
})
->where('active', '=', '1')
->get();

laravel eloquent query orWhere

In Normal PHP i have a sql query
$q = 'shojib'; or $q = '01913711...'; or $q = 'House 23...';
select * from member where LoginName like '%$q%' or mobile like '%$q%' or ConnectionAdderss like '%$q%' order by LoginName LIMIT 10;
i want to write this query in laravel.
Please help me.
i am using SoftDeletingTrait in laravel Member Model
Member::where('zone', 'like', '%'.$key.'%')
->orWhere('login_name', 'like', '%'.$key.'%')
->orWhere('mobile', 'like', '%'.$key.'%')
->orWhere('name', 'like', '%'.$key.'%')
->orderBy('login_name')
->take('100')
->get();
But it will return all data from database with deleted_at is not null;
it should return only those data whose deleted_at is null;
You should write the query as below
Member::onlyTrashed()->where('zone', 'like', '%'.$key.'%')
->orWhere('login_name', 'like', '%'.$key.'%')
->orWhere('mobile', 'like', '%'.$key.'%')
->orWhere('name', 'like', '%'.$key.'%')
->orderBy('login_name')
->take('100')
->get();
After certain times i have found solution
It should be
$members = Member::where(function($query) use ($key)
{
$query->where('zone', 'like', '%'.$key.'%')
->orWhere('login_name', 'like', '%'.$key.'%')
->orWhere('contact_number', 'like', '%'.$key.'%')
->orWhere('sms_comn', 'like', '%'.$key.'%')
->orWhere('email', 'like', '%'.$key.'%')
->orWhere('name', 'like', '%'.$key.'%');
})
->take('100')
->orderBy('login_name', 'ASC')
->get();

Left join not working properly

I'm trying to search all the users registered for a particular course using the following query using the laravel.
$users = DB::table('users')
->leftJoin('registrationrequests', 'users.id', '=', 'registrationrequests.user_id')
->where('firstname', 'LIKE', "%$search%")
->orWhere('lastname', 'LIKE', "%$search%")
->orWhere('username', 'LIKE', "%$search%")
->where('registrationrequests.course_id', $course_id)
->where('registrationrequests.registered', 1)
->get();
In the table registrationrequests, I have columns for course_id, user_id and registered (binary value). course_id and user_id are foreign keys for respective tables.
I'm getting an array of output. But it's not checking the condition where('registrationrequests.course_id', $course_id)
What might be the reason?
Using http://laravel.com/docs/4.2/queries#advanced-wheres and https://stackoverflow.com/a/18660266/689579 your query would look something like -
$users = DB::table('users')
->leftJoin('registrationrequests', 'users.id', '=', 'registrationrequests.user_id')
->where('registrationrequests.course_id', $course_id)
->where('registrationrequests.registered', 1)
->where(function($users) use($search){
$users->where('firstname', 'LIKE', "%$search%")
->orWhere('lastname', 'LIKE', "%$search%")
->orWhere('username', 'LIKE', "%$search%")
})
->get();

Categories