Laravel 5.2 whereRaw with muliple where conditions - php

below is my query . i want to get all results If (nic or mobile) and course matches ,
please advice
$conversations = Inquiries::whereRaw("( `course` = $request->input('course'))
AND( `mobile` = $request->input('mobile') OR 'nic', 'LIKE', '%'.$request->input('nic').'%')")
->get();

You need to properly create a query, like this:
$conversations = Inquiries::where('course', '=', $request->input('course'))
->where(function($query) use ($request) {
$query->where('mobile','=', $request->input('mobile'))
->orWhere('nic','LIKE','%'.$request->input('nic').'%');
})
->get();

Related

Laravel and PHP - Condition in the SQL request

I have a query
$BaseUsers = DB::table('users', 'users.ustatus', '=', '1')
->join('user_infos', 'user_infos.user_id', '=', 'users.id')
->select('users.*', 'user_infos.add1', 'user_infos.add3')
->paginate(3);
It work fine. But i need insert condition "where" to search in result
i send $request from form. How insert condition to query?
In PHP it looks like this:
if($request['add_1'])
{
$insert1=" and add_1<>'' ";
}
if($request['add_2'])
{
$insert2=" and add_2<>'' ";
}
...
$sql=" SELECT users.*, user_infos.add1, user_infos.add3 FROM users JOIN user_infos where users.ustatus=1 and user_infos.user_id=users.id ".$insert1." ".$insert2."";
How can this be done on laravel in Controller? I mean - just insert the necessary condition insert1, insert2 ... into the query
you can use where() and has() to build your query
$query = DB::table('users');
if($request->has('age')) {
$query->where('age', '>', $request->age);
}
$query = $query->get();
I hope it's helpful
All is simple ) I use when in query
$BaseUsers = DB::table('users', 'users.ustatus', '=', '1')
->join('user_infos', 'user_infos.user_id', '=', 'users.id')
->when($request->input('f_add3'), function ($query) {
return $query->where('user_infos.add3', '<>', '');
})
->select('users.*', 'user_infos.add1', 'user_infos.add3')
->paginate(3);

Laravel Eloquent - How to group conditions

I'm using the following eloquent statement
Title::where('word', 'lovers')
->orWhere('word', 'lover')
->where('domain', $resultFromFirstKeyword->domain)
->get();
Which I can see through the SQL logger produces the following SQL
SELECT * FROM `title`
WHERE `word` = 'lovers'
OR `word` = 'lover'
AND `domain` = 'www.texasbirdlovers.com'
However the SQL that I would like to produce is
SELECT * FROM `title`
WHERE (`word` = 'lovers' OR `word` = 'lover')
AND `domain` = 'www.texasbirdlovers.com'
How would I achieve this?
This should work:
Title::whereIn('word', ['lovers', 'lover'])
->where('domain', $resultFromFirstKeyword->domain)
->get();
If, however, you want to combine them exactly like you said, try this:
Title::where(function($query) {
$query->where('word', 'lovers')
->orWhere('word', 'lover');
})
->where('domain', $resultFromFirstKeyword->domain)
->get();
Just group your query in a where clause
Title::where(function($q){
$q->where('word', 'lovers')->orWhere('word', 'lover');
})->where('domain', $resultFromFirstKeyword->domain)->get();
Try
Title::where('domain', $resultFromFirstKeyword->domain)
->where(function($query) {
return $query
->where('word', '=', 'lovers')
->orWhere('word', '=', 'word');
})->get();
Try the following:
Title::where(function($query) {
return $query
->where('word', 'lover')
->orWhere('word', 'lovers');
})
->where('domain', $resultFromFirstKeyword->domain)
->get();

How to use where not between in Laravel 5.5?

I am try
ing to get something like this
select * from `users`
inner join `settings`
on `users`.`id` = `settings`.`user_id`
and NOW() NOT BETWEEN quit_hour_start AND quit_hour_end
where `notification_key` != ''
and `device_type` = 'Android'
in eloquent. Does anyone try and get success to build this query in eloquent.
I know I can use \DB::select(DB::raw()); and get my result. But I want to use ie with Laravel eloquent method.
====== update comment for tried queries========
$androidUser = User::join('settings', function ($join) {
$join->on('users.id', '=', 'settings.user_id')
->where(DB::raw("'$currentTime' NOT BETWEEN quit_hour_start AND quit_hour_end"));
})
->where('notification_key', '!=', '')
->where('device_type' ,'=', 'Android')
->get();
$users = DB::table('users')
->whereNotBetween('votes', [1, 100]) // For one column
->whereRaw("? NOT BETWEEN quit_hour_start AND quit_hour_end", [$currentTime]) // Use whereRaw for two columns
->get();
https://laravel.com/docs/5.5/queries, or you can rewrite as to wheres

Laravel 5.4 subquery in where condition?

I want to write sub query inside where in condition and in subquery were condition, checking with parent query field.
as follows,
$query = DB::table('users');
$query->whereNotIn('users.id', function($query) use ($request) {
$query->select('award_user.user_id')
->from('award_user')
->where('award_user.user_id', 'users.id')
->where('award_user.award_id', $request->award_id);
});
Query is working fine, but
->where('award_user.user_id', 'users.id')
This line, users.id is not taking from parent query. If I enter manually number, then it is working correctly.
What is wrong with my query.. can you please suggest.
use whereRaw rather than where
$query = DB::table('users');
$query->whereNotIn('users.id', function($query) use ($request) {
$query->select('award_user.user_id')
->from('award_user')
->whereRaw('award_user.user_id', 'users.id')
->whereRaw('award_user.award_id = '.$request->award_id);
});

If using a query builder as to what in the form of its laravel?

I have a query sql like the following may be a bit complex, I am a bit of trouble to convert into a for framework laravel. Please help for everything with framework laravel query results. My problem here is, I do not know how to create a sub select query to laravel framework. Thanks guys.
SELECT
lin_users.status_employee_id,
lin_users.id,
lin_users.username,
lin_users.created,
lin_users.modified,
lin_employee_attributes.unit_code,
lin_employee_attributes.position_code,
lin_employee_attributes.begin_date,
lin_employee_attributes.end_date,
contactnumber.contact_id as phone_number,
contactmobile.contact_id as cell_number,
contactemail.contact_id as email
FROM lin_users
INNER JOIN lin_status_employees
ON lin_users.status_employee_id = lin_status_employees.id
INNER JOIN lin_people
ON lin_status_employees.person_id = lin_people.id
INNER JOIN lin_employee_attributes
ON lin_users.status_employee_id = lin_employee_attributes.status_employee_id
LEFT JOIN lin_contacts AS contactnumber
ON lin_people.id = contactnumber.person_id AND contactnumber.contact_type = 'Work Telephone'
LEFT JOIN lin_contacts AS contactmobile
ON lin_people.id = contactmobile.person_id AND contactmobile.contact_type = 'Mobile'
LEFT JOIN lin_contacts AS contactemail
ON lin_people.id = contactemail.person_id AND contactemail.contact_type = 'Email'
WHERE lin_employee_attributes.begin_date = '2016-11-07'
OR lin_employee_attributes.end_date = '2017-10-21'
GROUP BY lin_users.id,
lin_employee_attributes.unit_code,
lin_employee_attributes.position_code,
lin_employee_attributes.begin_date,
lin_employee_attributes.end_date, lin_people.id,
contactnumber.contact_id,
contactmobile.contact_id,
contactemail.contact_id;
Try this:
const TABLE = 'my_table_name';
return $this
->select(
self::TABLE . 'id as myidalias',
self::TABLE . 'username as myuseralias')
->addSelect(DB::raw(
"
(your custom select here) as mycustomresult
"
));
You can add as many addSelect as you want. Also when you have a lot of complex queries like this, a lot of times you have duplicated parts on them so I highly recommend using scopes it makes your code clean and reusable.
public function scopeLeftJoinCategory($query)
{
return $query
->leftJoin(Category::CONTENTS_CATEGORIES . ' AS cc', 'con.id', '=', 'cc.content_id')
->leftJoin(Category::TABLE . ' AS cat', 'cc.category_id', '=', 'cat.id');
}
Then you just use it like this: ->leftJoinCategory()
Using this work. Finally, with the above query can be solved with the concept query to query in laravel.
$users = $this->db->getTable('users')
->select('users.status_employee_id',
'users.id',
'users.username',
'users.email',
'users.created',
'users.modified',
'users.flag_delete',
'employee_attributes.unit_code',
'employee_attributes.position_code',
'employee_attributes.begin_date',
'employee_attributes.end_date',
'contactnumber.contact_id as phone_number',
'contactmobile.contact_id as cell_number',
'contactemail.contact_id as email'
)
->join('status_employees', 'users.status_employee_id', '=', 'status_employees.id')
->join('people', 'status_employees.person_id', '=', 'people.id')
->join('employee_attributes', 'users.status_employee_id', '=', 'employee_attributes.status_employee_id')
->leftJoin('contacts AS contactnumber', function($join)
{
$join->on('people.id', '=', 'contactnumber.person_id');
$join->on('contactnumber.contact_type','=', DB::raw("'Work Telephone'"));
})
->leftJoin('contacts AS contactmobile', function($join)
{
$join->on('people.id', '=', 'contactmobile.person_id');
$join->on('contactmobile.contact_type','=', DB::raw("'Mobile'"));
})
->leftJoin('contacts AS contactemail', function($join)
{
$join->on('people.id', '=', 'contactemail.person_id');
$join->on('contactemail.contact_type','=', DB::raw("'Email'"));
})
->where(function ($query) use ($begin_date, $end_date) {
$query->where('employee_attributes.begin_date', $begin_date)
->orWhere('employee_attributes.end_date', $end_date);
})
->groupby('users.status_employee_id',
'users.id',
'users.username',
'users.email',
'users.created',
'users.modified',
'users.flag_delete',
'employee_attributes.unit_code',
'employee_attributes.position_code',
'employee_attributes.begin_date',
'employee_attributes.end_date',
'contactnumber.contact_id',
'contactmobile.contact_id',
'contactemail.contact_id'
)
->get();

Categories