Pretty much I want the query to select all records of users that are 25 years old AND are either between 150-170cm OR 190-200cm.
I have this query written down below. However the problem is it keeps getting 25 year olds OR people who are 190-200cm instead of 25 year olds that are 150-170 OR 25 year olds that 190-200cm tall. How can I fix this? thanks
$heightarray=array(array(150,170),array(190,200));
$user->where('age',25);
for($i=0;$i<count($heightarray);i++){
if($i==0){
$user->whereBetween('height',$heightarray[$i])
}else{
$user->orWhereBetween('height',$heightarray[$i])
}
}
$user->get();
Edit: I tried advanced wheres (http://laravel.com/docs/queries#advanced-wheres) and it doesn't work for me as I cannot pass the $heightarray parameter into the closure.
from laravel documentation
DB::table('users')
->where('name', '=', 'John')
->orWhere(function($query)
{
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
Like Jeemusu and the OP stated, you need to use advance wheres.
But if you want to pass a variable to the closure function you need to make use of the "use" approach:
$heightarray = array(array(150,170),array(190,200));
DB::table('users')
->where('name', '=', 'John')
->orWhere(function($query) use ($heightarray){
//...
})
->get();
I found the answer. I need to include "use" in the closure to pass my $heightarray variable in. Once $heightarray is in then laravel's advance wheres work.
$heightarray=array(array(150,170),array(190,200));
$user->where('age',25);
$userprofile->Where(function($query) use ($heightarray) {
for($i=0;$i<count($heightarray);i++){
if($i==0){
$user->whereBetween('height',$heightarray[$i])
}else{
$user->orWhereBetween('height',$heightarray[$i])
}
}
});
$user->get();
This is completely untested, but looking at the documentation for advance wheres, it would seem you want to try something like this:
DB::table('users')
->where('age',25)
->Where(function($query)
{
for($i=0;$i<count($heightarray);i++){
if($i==0){
$query->whereBetween('height', $heightarray[$i]);
}else{
$query->orWhereBetween('height', $heightarray[$i]);
}
}
})->get();
Related
$checking = DB::table('bookings')->whereBetween('checkindate', [$checkindate, $checkoutdate])->first();
------My first query--------
$checking_2 = DB::table('bookings')->where('checkindate',$checkindate)->where('checkoutdate',$checkoutdate)->first();
--------My Second query-------
$checking_3 = DB::table('bookings')->whereBetween('checkoutdate',[$checkindate,$checkoutdate])->first();
------ My third query-------------
I want to build these queries into a single query but unable to do it can anybody help it with me.
I want to run each query and store the result in a single array .
Main question how to use OR after wherebetween query because if I simply use ->Where it doesn't give me desire result
or can anywrite this code in a single query?
Try
$camps = DB::table('bookings')->where(function ($q)
{
$q->whereBetween('checkindate', [$checkindate, $checkoutdate])
->orWhere(function($q1) {
$q1->where('checkindate',$checkindate)->where('checkoutdate',$checkoutdate)
})
->orwhereBetween('checkoutdate', [$checkindate, $checkoutdate])
})->first();
Try this code.
$checking = DB::table('bookings')
->whereBetween('checkindate', [$checkindate, $checkoutdate])
->orWhereBetween('checkoutdate',[$checkindate,$checkoutdate])
->orWhere(function($query){
$query->where('checkindate',$checkindate)->where('checkoutdate',$checkoutdate)
})
->first();
You can use the following query to get the result.
->where(function($query) use($checkindate, $checkoutdate) {
$query->whereBetween('checkindate', [$checkindate, $checkoutdate])
->orWhere(function($q) use($checkindate, $checkoutdate) {
$q->where('checkindate',$checkindate)->where('checkoutdate',$checkoutdate)
})
->orWhereBetween('checkoutdate',[$checkindate,$checkoutdate])
})->first();
use this one
$checking_2 = DB::table('bookings')->where('checkindate','>=' ,$checkindate)->where('checkoutdate','<=',$checkoutdate)->get();
I'm trying filtered a collection, but the code in the last row does not work. In this row the $property->county_id is an integer, the params.county_id is an array. I would like know the array contains the integer.
I think the code is wrong, because the key (maybe) must be the params.county_id. How I can do this?
Thanks the answers.
$buyerSearches = collect($items);
$result = $buyerSearches
->where('params.type', '=', $property->type)
->where('params.sale_type', '=', $property->sale_type)
->whereIn('params.contract_type', ['all', $property->contract_type])
->where('params.min_price', '<=', $property->price)
->where('params.max_price', '>=', $property->price)
->whereIn($property->county_id, 'params.county_id');
Be sure that the second argument in the whereIn is an array or create that array before the $result query and use it next.
I solved, it works :)
$result = $buyerSearches
->where('params.type', '=', $property->type)
->where('params.sale_type', '=', $property->sale_type)
->whereIn('params.contract_type', ['all', $property->contract_type])
->where('params.min_price', '<=', $property->price)
->where('params.max_price', '>=', $property->price)
->filter(function($buyerSearch) use ($property) {
return in_array($property->county_id, $buyerSearch['params']['county_id']);
});
Try this code instead :
->whereIn('county_id', 'params.county_id');
My Requirements : I select baseleads table with Random Order using following conditions
beaseleads currentstatus column value have New Lead Status or
beaseleads currentstatus column value have Call Not Picking status and updated_at date is not Equal to Today Date.
My Laravel Query is below
$baseleadsData=Baseleads::inRandomOrder()->Where('process_status',0)->where(function ($query) {
$query->where('current_status','New Lead');
$query->Orwhere('current_status','Call Not Picking');
$query->OrwhereDate('updated_at', '!=', date('Y-m-d'));
})->first();
Its not Working Properly. What is my Mistake.
->where(function ($query) {
$query->where('current_status','New Lead')
->orWhere('current_status','Call Not Picking')
->orWhere('updated_at', '!=', date('Y-m-d').' 00:00:00');})->first();
It's been a while since I used Laravel, but have you tried
$query->where('current_status', '=', 'New Lead');
$query->Orwhere('current_status', '=', 'Call Not Picking');
instead of
$query->where('current_status','New Lead');
$query->Orwhere('current_status','Call Not Picking');
I'm not sure this will work as it looks like Laravel automatically makes the comparison. What is the error code youre getting? (You can turn debug mode on in your settings file)
you need to use method names in camelCase
use orWhere and orWhereDate
You did mistake of method name, use Orwhere to orWhere, OrwhereDate to orWhereDate and Where to where. and orWhereDate outside of where query.
$baseleadsData=Baseleads::inRandomOrder()
->where('process_status',0)
->where(function ($query) {
$query->where('current_status','New Lead');
$query->orWhere('current_status','Call Not Picking');
})
->orWhereDate('updated_at', '!=', date('Y-m-d'))
->first();
I'm trying to retrieve a number of results from database using eloquent.
I dont have problem with that.
Example code
$flights = App\Flight::where('active', 1)
->orderBy('name', 'desc')
->take(10)
->get();
Now lets suppose I have a variable that if it's true i want to add one more WHERE clause. For example if $domestic == true then i want something like this:
$flights = App\Flight::where('active', 1)
->where('domestic',1)
->orderBy('name', 'desc')
->take(10)
->get();
So I dont like to do, because it's not nice.
if($domestic) {
$flights = App\Flight::where('active', 1)
->where('domestic',1)
->orderBy('name', 'desc')
->take(10)
->get();
} else {
$flights = App\Flight::where('active', 1)
->orderBy('name', 'desc')
->take(10)
->get();
}
Ideally i want to pass only the where clause eg.
if(#domestic) { $flights->where('domestic',1) }
But this is not working.
What is the best way to pass additional where clauses whenever needed?
$flights = App\Flight::where('active', 1)
->orderBy('name', 'desc')
->when($domestic, function ($query){
return $query->where('domestic',1)
})
->take(10)
->get();
The laravel query builder has some real gems like the when clause, the when clause will only execute the closure if the first parameter is true (in this case $domestic)
This specific function can be found here. On the same way more of these functions can be found.
Solution is:
$flights = App\Flight::where('active', 1);
if($domestic) {
$flights->where('domestic',1);
}
$results = $flights->orderBy('name', 'desc')
->take(10)
->get();
Use Query Scopes
Write them on your model, quick example on flight model:
public function scopeIsDomestic($query,$domestic){
if($domestic == 1){
return $query->where('domestic',1);
}else{
return $query; //you can return the inverse if you need it -> where domestic <> 1
}
}
And now use it like this
$flights = App\Flight::where('active', 1)
->isDomestic(1)
->orderBy('name', 'desc')
->take(10)
->get();
It provides a nice way to keep the code maintainable since you can update the scope on all your queries at the same time by modifying it on a single place
In my controller I have wrote this following code
$usersCount = User::where('activated', '=', 1)->where('group_id', '=', 1)->where('availability_date', '<=', $opportunity_date)->count();
$locations_array_result = explode(",",$locations_array_result);
foreach ($locations_array_result as $param)
{
$usersCount = $usersCount->whereHas('location', function($q) use($param){
$q->where('location_id', '=', $param );
});
}
This code giving following error
Call to a member function whereHas() on a non-object
Can anyone help me to find out what i have done wrong!!!
$usersCount is already a number from the 1st line of your sample.
You want instead to replace $usersCount->whereHas with User::whereHas in your foreach loop.
Taking a very wild guess here, I would think you need to get all users with these requirements
->where('group_id', '=', 1)->where('availability_date', '<=', $opportunity_date)
plus having a location_id value which exists on an array named $locations_array_result
If this is the case, this is all you need:
User::where('activated', '=', 1)->where('group_id', '=', 1)->where('availability_date', '<=', $opportunity_date)->whereIn('location_id', $locations_array_result)->get();
EDIT
Following your comment below, I assume user has many to many relation with locations (defined in your model), so eager loading and then using a condition with a callback should do the job:
$users = User::where('activated', '=', 1)
->where('group_id', '=', 1)
->where('availability_date', '<=', $opportunity_date)
->with(array('location' => function($query) use ($locations_array_result)
{
$query->whereIn('location_id', $locations_array_result);
}))->get();