I have this query:
SELECT * FROM table
WHERE ( name = '%alex%'
OR address = '%alex%'
OR lastname = '%alex%'
) AND id = '%alex%'
AND email = '%alex%'
How I can convert above SQL query into Laravel Query builder standard?
Illuminate\Support\Facades\DB::table('table')
->where('name', 'LIKE', '%alex%')
->orWhere('address', 'LIKE', '%alex%')
->orWhere('lastname', 'LIKE', '%alex%')
->where('id', 'LIKE', '%alex%')
->where('email', 'LIKE', '%alex%')
->get();
Related
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();
My query like :
$results = User::
where('this', '=', 1)
->where('that', '=', 1)
->where('this_too', '=', 1)
->where('that_too', '=', 1)
->where('this_as_well', '=', 1)
->where('that_as_well', '=', 1)
->where('this_one_too', '=', 1)
->where('that_one_too', '=', 1)
->where('this_one_as_well', '=', 1)
->where('that_one_as_well', '=', 1)
->get();
Solution for the multi where condition in laravel ,because its not working currently
->where('twd.status','=','0')->where(function($q) use($data){
$q->where('A','like', '%' .$data. '%')
->orWhere('B','like', '%' .$data. '%');
->orWhere('C','like', '%' .$search_param. '%');
})
You can do it like that .
Concat all column that you want to search
->where(DB::raw("concat(A,' ',B)"), 'like', "%".$data."%");
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();
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();
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();