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');
Related
here is what I want
check rows with ( user_id AND member_name)
I have try some other code and in this one I got this error
"message":"mb_strpos() expects parameter 1 to be string, object given"
$checkData= DB::table('member_opinions')
->where(DB::table('member_opinions')
->where('user_id',$members->user_id)
)
->orWhere( DB::table('member_opinions')
->where('member_name',$request->{'committees_name'})
)
->first();
/* $checkData= DB::table('member_opinions')
->where('user_id',$members->user_id)
->orWhere('member_name',$request->{'committees_name'})
->first();
*/
How can I do it?
$checkData= DB::table('member_opinions')
->where('user_id',$members->user_id)
->orWhere('member_name',$request->{'committees_name'})
->first();
Let me know did it works.
Update:
Try it out if you want and operation
$checkData= DB::table('member_opinions')
->where('user_id',$members->user_id)
->where('member_name',$request->{'committees_name'})
->first();
That's because of you try to provide object in where() function instead of string.
You have to extract your conditions from inner where() functions and provide them directly to outer where() functions like this:
$checkData = DB::table('member_opinions')
->where('user_id', $members->user_id)
->orWhere('member_name', $request->{'committees_name'})
->first();
After that, pay attention on what you want to get. As I can see, you want to use AND condition, but used orWhere() function, which will build your query with OR condition. So, you should use andWhere() instead:
$checkData = DB::table('member_opinions')
->where('user_id', $members->user_id)
->andWhere('member_name', $request->{'committees_name'})
->first();
And finally, let's make your code less dirty:
$userId = $members->user_id;
$commiteesName = $request->committees_name;
$checkData = DB::table('member_opinions')
->where('user_id', $userId)
->andWhere('member_name', $commiteesName)
->first();
UPD: #user3532758 is right, it is no method called andWhere() in Laravel, you should use where() instead:
$userId = $members->user_id;
$commiteesName = $request->committees_name;
$checkData = DB::table('member_opinions')
->where('user_id', $userId)
->where('member_name', $commiteesName)
->first();
I want to get the template from user_webhook table in my database.In WHERE condition i am checking user_id,app_id and if either notify_admin or notify_customer value is 1 in user_webhook table.I am using query..
$templates= $this->where('notify_admin',1)
->orwhere('notify_customer',1)
->where('user_webhooks.user_id',$user_id)
->where('user_webhooks.app_id',$app_id)
->select( 'webhooks.id as webhook_id','webhooks.app_id','webhooks.topic','webhooks.type','webhooks.action',
'webhooks.sms_template','user_webhooks.id','user_webhooks.notify_admin',
'user_webhooks.notify_customer','user_webhooks.user_id','user_webhooks.sms_template_status',
'user_webhooks.sms_template as sms'
)
->join ('webhooks',function($join){
$join>on('webhooks.id','=','user_webhooks.webhook_id');
})
->get()
->toArray();
when i get query using DB::getQueryLog(), I found the query seems Like
select `telhok_webhooks`.`id` as `webhook_id`, `telhok_webhooks`.`app_id`,
`telhok_webhooks`.`topic`, `telhok_webhooks`.`type`, `telhok_webhooks`.`action`,
`telhok_webhooks`.`sms_template`, `telhok_user_webhooks`.`id`,
`telhok_user_webhooks`.`notify_admin`, `telhok_user_webhooks`.`notify_customer`,
`telhok_user_webhooks`.`user_id`, `telhok_user_webhooks`.`sms_template_status`,
`telhok_user_webhooks`.`sms_template` as `sms` from `telhok_user_webhooks`
inner join
`telhok_webhooks` on `telhok_webhooks`.`id` = `telhok_user_webhooks`.`webhook_id`
where `notify_admin` = ? or `notify_customer` = ? and `telhok_user_webhooks`.`user_id`
= ? and `telhok_user_webhooks`.`app_id` = ?
The result of query giving result of all app_id and user_id.
So Please tell me use of OR in where condition.
Thanks in advance.
You may chain where constraints together as well as add or clauses to the query. The orWhere method accepts the same arguments as the where method:
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere('name', 'John')
->get();
Advanced usage:
Usere::where('id', 46)
->where('id', 2)
->where(function($q) {
$q->where('Cab', 2)
->orWhere('Cab', 4);
})
->get();
The whereIn method verifies that a given column's value is contained within the given array:
$users = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();
More: https://laravel.com/docs/5.5/queries
Change
->where('notify_admin',1)
->orwhere('notify_customer',1)
to
->where(function($q){
$q->where('notify_admin',1)
->orWhere('notify_customer',1);
})
Without this, the orWhere will compare to all other wheres in your query instead of just comparing those two columns
I am trying to get a result set from a laravel eloquent query whereby I match a column against a list of values in an array.
$authenticated_operation_ids = AccessControl::where('user_id', '=', $user_id)
->where('entity_type_id', '=', $operation_entity_id)
->pluck('entity_access_id')->toArray();
$authenticated_operations = Operation::whereIn('id', $authenticated_operation_ids);
return view('page.index')->withOperations($authenticated_operations);
You can try it as:
$authenticated_operation_ids = AccessControl::where('user_id', '=', $user_id)->where('entity_type_id', '=', $operation_entity_id)->pluck('entity_access_id')->toArray();
$authenticated_operations = Operation::whereIn('id', $authenticated_operation_ids)->get();
return view('page.index')->withOperations($authenticated_operations);
Add get() at the end of the query.
1) pluck returns a single value from a single row. You want lists to get a single column from multiple rows. toArray may not be needed, as it returns an array of values
$authenticated_operation_ids = AccessControl::where('user_id', '=', $user_id)
->where('entity_type_id', '=', $operation_entity_id)
->lists('entity_access_id');
2) You're forgetting to actually retrieve the rows in your second line:
$authenticated_operations = Operation::whereIn('id', $authenticated_operation_ids)
->get();
You have to call get() function on the result set to obtain results. The modified code will be like
$authenticated_operation_ids = AccessControl::where('user_id', '=', $user_id)->where('entity_type_id', '=', $operation_entity_id)->get()->pluck('entity_access_id');
$authenticated_operations = Operation::whereIn('id', $authenticated_operation_ids)->get();
return view('page.index')->withOperations($authenticated_operations);
or you can use a cursor to process it.
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();
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();