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();
Related
I am trying to select rows from table students using laravel query for searching but on searching it's returning all rows from table.
$data = DB::table('students')
->select(['name', 'username'])->where('institute', $inst)
->where('username', 'LIKE', '%'.$search.'%')
->orWhere('name', 'LIKE', '%'.$search.'%')
->orWhere('email', 'LIKE', '%'.$search.'%')
->orWhere('contact', 'LIKE', '%'.$search.'%')
->orderBy('id', 'DESC')
->paginate(10);
This is the thing, at least for what I can see, you have two kind of filters:
Students that are in a given institute
Students that have a similar username, name, email or contact than the given search term
If so, the problem in your query are the orWhere() clauses. Let's simplify your query to illustrate the issue:
$data = DB::table('students')
->where('institute', $inst)
->orWhere('name', 'LIKE', '%'.$search.'%')
->get();
In the above code, the statement will get all the students that have the institute equals to $inst OR have a name similar to %search%. So, the results could include students that are not in the given institute. You see the issue now?
In order to first filter by institute AND get records that have a similar similar username, name, email or contact, you could wrap the second conditions in a closure. Try this:
$data = DB::table('students')
->select(['name', 'username'])
->where('institute', $inst)
->where(function ($query) use ($search) { // <<<
$query->where('username', 'LIKE', '%'.$search.'%')
->orWhere('name', 'LIKE', '%'.$search.'%')
->orWhere('email', 'LIKE', '%'.$search.'%')
->orWhere('contact', 'LIKE', '%'.$search.'%');
})
->orderBy('id', 'DESC')
->paginate(10);
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();
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();
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();
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();