Trying to query a model with a lower level relationship (agreement) and I cannot get chaining to work. I tried the three methods below (the second get me 99% of the way there, but it misses out on one of the results - an inactive equipment "status" and an "inactive" agreement (should have been omitted). What am I doing wrong?
Every piece of equipment needs to be "active" and have an active existing agreement (an active agreement is one that is either "Active", "Cancel on expiry" or "Unsigned").
$thing = Equipment::wherehas('agreement', function($q)use($month)
{
$q->where('equipment.status', '=', 'Active')
->where('maintenance_months', 'like', '%'.$month.'%')
->where('status', '=', 'Active')
->orWhere('status', '=', 'Unsigned')
->orWhere('status', '=', 'Cancel on expiry');
})->Get();
$thing = Equipment::wherehas('agreement', function($q)use($month)
{
$q->where('status', '=', 'Active')
->orWhere(function($q2)use($month)
{
$q2->where('equipment.status', '=', 'Active')
->where('maintenance_months', 'like', '%'.$month.'%');
})
->where('status', '=', 'Unsigned')
->orWhere(function($q3)use($month)
{
$q3->where('equipment.status', '=', 'Active')
->where('maintenance_months', 'like', '%'.$month.'%');
})
->where('status', '=', 'Cancel on expiry')
->orWhere(function($q4)use($month)
{
$q4->where('equipment.status', '=', 'Active')
->where('maintenance_months', 'like', '%'.$month.'%');
});
})->Get();
$thing = Equipment::wherehas('agreement', function($q)use($month)
{
$q->where('status', '=', 'Active')
->where('equipment.status', '=', 'Active')
->where('maintenance_months', 'like', '%'.$month.'%')
->orWhere('status', '=', 'Unsigned')
->where('equipment.status', '=', 'Active')
->where('maintenance_months', 'like', '%'.$month.'%')
->orWhere('status', '=', 'Cancel on expiry')
->where('equipment.status', '=', 'Active')
->where('maintenance_months', 'like', '%'.$month.'%');
})->get();
i am not quite understand why the where equipment.status is being filter for the same thing over and over again, if i am not understand wrongly the following code might be the result you are looking for.
$thing = Equipment::whereHas('agreement', function($q)use($month)
{
$q->where('maintenance_months', 'like', '%'.$month.'%')
->whereIn('status',['Unsigned','Active','Cancel on Expiry'])
})->where('status','Active')->get();
Related
My laravel "where" is getting ignored it seems. ->where('products.hide_product', '=', 'N')
Any ideas on what I am doing wrong?
$products = Product::join('brands','products.brand_id','=','brands.brand_id')
->join('categories','products.cat_id','=','categories.cat_id')
->leftJoin('images','products.product_id','=','images.product_id')->where('img_priority','=','1')->orWhere('img_priority', '=', null)
->where('products.hide_product', '=', 'N')->where('products.group_id', '=', $groupID)->orderBy('img_priority','DESC')
->get(array('products.en_71','products.astm','images.file_name','products.product_id','products.product_name','products.collect_part_no','brands.brand_name','categories.cat_name','products.status','images.file_type','products.pop','products.color_label','products.ai_complete'));
You can move the join in a closure an use the first where there:
$products = Product::join('brands', 'products.brand_id', '=', 'brands.brand_id')
->join('categories','products.cat_id','=','categories.cat_id')
->leftJoin('images', function ($join) {
$join->on('products.product_id','=','images.product_id')
->where('img_priority','=','1')
->orWhere('img_priority', '=', null);
})
->where(
['products.hide_product', '=', 'N'],
['products.group_id', '=', $groupID],
)
->select('products.en_71', 'products.astm', 'images.file_name', 'products.product_id', 'products.product_name', 'products.collect_part_no', 'brands.brand_name', 'categories.cat_name', 'products.status', 'images.file_type', 'images.img_priority', 'products.pop', 'products.color_label', 'products.ai_complete')
->orderBy('img_priority', 'DESC')
->get();
Please try below code.
$products = Product::join('brands','products.brand_id','=','brands.brand_id')
->join('categories','products.cat_id','=','categories.cat_id')
->leftJoin('images', function($join)) {
$join->on('products.product_id','=','images.product_id')
->where(function($query){
$query->where('img_priority','=','1')
->orWhere('img_priority', '=', null)
})
}
->where(function($query) {
$query->where('products.hide_product', '=', 'N')
->where('products.group_id', '=', $groupID)
})
->orderBy('img_priority','DESC')
->get(array('products.en_71','products.astm','images.file_name',
'products.product_id','products.product_name',
'products.collect_part_no', 'brands.brand_name',
'categories.cat_name', 'products.status',
'images.file_type','products.pop','products.color_label',
'products.ai_complete')
);
I use this : https://github.com/rinvex/repository
My query is like this :
$query = $this->store_repository
->join('favorites', function ($join) {
$join->on('stores.id', '=', 'favorites.favoritable_id')
->where('favorites.favoritable_type', 'like', 'App\\\Models\\\Store');
})
->where('stores.status', '=', 1)
->select('stores.id', 'stores.name', 'stores.photo','stores.address');
if($location)
$query->where('stores.address', 'like', "%$location%");
if($q) {
$query->where('stores.name', 'like', "%$q%")
->where('stores.address', 'like', "%$q%", 'or');
}
$result = $query->orderBy('favorites.updated_at', 'desc')->paginate($num);
When executed, there exist error like this :
Missing argument 3 for
Rinvex\Repository\Repositories\BaseRepository::join()
How can I solve it?
I don't think you need the clojure to make what you want.
I guess this should be fine :
$query = $this->store_repository
->join('favorites', '=', 'favorites.favoritable_id')
->where('favorites.favoritable_type', 'like', 'App\\\Models\\\Store');
->where('stores.status', '=', 1)
->select('stores.id', 'stores.name', 'stores.photo','stores.address');
Given this code:
$objResellerList - DB::table('ResellerMaster as RM')
->leftJoin('StateMaster as SM','SM.id',RM.id_StateMaster')
->leftJoin('RegionMaster as REM','REM.id','=','RM.id_RegionMaster')
->leftJoin('DealerGroupMaster as DGM','DGM.id','=','RM.id_DealerGroupMaster')
->when($keywords, function($query) use ($keywords) {
return $query->where('RM.company_name', 'like', '%'.$keywords.'%')
->orWhere('DGM.name', 'like', '%'.$keywords.'%')
->orWhere('RM.email', 'like', '%'.$keywords.'%')
->orWhere('RM.pic', 'like', '%'.$keywords.'%')
->orWhere('RM.pic_mobile_no', 'like', '%'.$keywords.'%');
})
->when($status, function($query) use ($status) {
return $query->where('RM.status', $status);
})
->select('RM.*','SM.name as state','REM.name as region', 'DGM.name as dealer_group')
->orderby('RM.company_name','asc')
->paginate(10);
When i search with keywords, I need to close it with a bracket at when condition. How to include the bracket at when condition?
DB::table('users')
->where('name', '=', 'John')
->orWhere(function ($query) {
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
give u brackets
select * from users where name = 'John' or (votes > 100 and title <> 'Admin')
so use where with function
->orWhere(function ($query) {
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
or
->where(function ($query) {
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
it will give u brackets
use
->when($status,function($query) {
return $query->where(function ($query) {
//do some action
})
})
I have this query:
SELECT * FROM users
WHERE
gender = "$gender" AND country = "$country"
AND ((city = "$city" AND status = "1")
OR (status IN(2,3)))";
How can I write above query in Lumen ?
What I have tried so far is :
$users = User::where('country', '=', $country)
->where('gender', '=', $gender)
->where(function ($users) {
$users->where('city', '=', '$city')
->where('status', '=', "1");
})
->whereIn('status', [2, 3])
->first();
But this query doesn't returns the expected result.
Any idea what is the fault in my query?
Try the following code.
$users = User::where('country', '=', $country)
->where('gender', '=', $gender)
->where(function ($query) use ($city) {
$query->where(function($query) use ($city) {
$query->where('city', '=', '$city')
->where('status', '=', "1");
})->orWhereIn('status', [2, 3]);
})->first();
I'm using laravel 5 in current project. Here is my query to database:
$users = User::where('status', '=', '3')->orWhere('status', '=', '2')->whereExists(function($query1)
{
$query1->select(DB::raw(1))
->from('firsts')
->whereRaw('firsts.user_id = users.id')
->where('status', '=', 'approved');
})
->whereExists(function($query2)
{
$query2->select(DB::raw(1))
->from('seconds')
->whereRaw('seconds.user_id = users.id')
->where('status', '=', 'approved');
})
->whereExists(function($query3)
{
$query3->select(DB::raw(1))
->from('thirds')
->whereRaw('thirds.user_id = users.id')
->where('status', '=', 'approved');
})
->whereExists(function($query4) use($category_id)
{
$query4->select(DB::raw(1))
->from('jobcategories')
->where('provider_id', '!=', 0)
->where('category_id', '=',$category_id);
})
->get();
Idea behind that is to pick all valid users. As you can see at the begining of query I wrote first condition for users. The problem is that users with status = '2' will never have their thirds table status = 'approved'. Is there possibility to put if statment before $query3? Thanks in advance!
Try the query below
$users = User::whereIn('users.status', array(2, 3))
->leftJoin('firsts f', 'f.user_id', '=', 'users.id')
->leftJoin('seconds s', 's.user_id', '=', 'users.id')
->leftJoin('thirds t', 't.user_id', '=', 'users.id')
->where(function($query) {
$query->where('users.status', 2);
$query->where('f.status', 'approved');
$query->where('s.status', 'approved');
})
->orWhere(function($query) {
$query->where('users.status', 3);
$query->where('f.status', 'approved');
$query->where('s.status', 'approved');
$query->where('t.status', 'approved');
});
I'm not sure what to do with the last part of your query, as it has no relation to any of users, firsts, seconds, thirds:
->whereExists(function($query4) use($category_id)
{
$query4->select(DB::raw(1))
->from('jobcategories')
->where('provider_id', '!=', 0)
->where('category_id', '=',$category_id);
})