two condition in same query laravel - php

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();

Related

Collection WhereIn method

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');

Laravel and SQL wherein Orwhere where doesn't restrict

I think this is an easy one, but driving me a bit crazy
I have this:
Transaction::
where('seller_id', $this->id)->
Orwhere('buyer_id', $this->id )->
whereIn('concept', ['Lemonway','Paypal'])->
where('status', '=', 'ok')
->get();
So if I'm clearly specifying status = 'ok'
Why is picking a status = Pending transaction?
You need to use the where() closure for parameter grouping, for example:
Transaction::where(function($q) {
$q->where('seller_id', $this->id)
->orwhere('buyer_id', $this->id)
->whereIn('concept', ['Lemonway', 'Paypal']);
})
->where('status', 'ok')
->get();

Laravel Model Using Or in where Condition?

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

Laravel 5.2 Query Builder get as a single array(not 2D or 3D)

This is my query.
$mem = DB::table('user_zakathorg')
->JOIN('zakathorgs', 'user_zakathorg.zakathorg_id', '=', 'zakathorgs.ID')
->WHERE('user_id', '=', Auth::user()->id )
->SELECT('zakathorg_id', 'orgName')->get();
And the output is always one raw. like following image
If i want to accesss the zakathorg_id. I have to use a foreach loop. And I can't access
$mem[0]['zakathorg_id]
how can i use this as simply $mem['zakathorg_id']
Use first() method instead of get():
$mem = DB::table('user_zakathorg')
->JOIN('zakathorgs', 'user_zakathorg.zakathorg_id', '=', 'zakathorgs.ID')
->WHERE('user_id', '=', Auth::user()->id )
->SELECT('zakathorg_id', 'orgName')->get();
// You can access $mem->zakathorg_id now

laravel using query builder without chaining

I have a query builder that works:
$article = Page::where('slug', '=', $slug)
->where('hide', '=', $hidden)
->first();
But I want to only add the second where statement if hidden is equal to 1. I've tried the code below which shows the logic of what I'm trying to do, but it doesn't work.
$article = Page::where('slug', '=', $slug);
if ($hidden == 1) {
$article->where('hide', '=', 1);
}
$article->first();
I'm using Laravel 4, but I think the question still stands with Laravel 3.
Yeah there's a little "gotcha" with Eloquent and the query builder. Try the code below ;)
$query = Page::where('slug', '=', $slug);
if ($hidden == 1) {
$query = $query->where('hide', '=', 1);
}
$article = $query->first();
Note the assigning of $query within the conditional. This is becuase the first where (statically called) returns a different object to the query object within the conditional. One way to get around this, I believe due to a recent commit, is like so:
$query = Page::where('slug', '=', $slug)->query();
This will return the query object and you can do what you want as per normal (Instead of re-assigning $query).
Hope that helps.

Categories