$q = Order::select(
'orders.*',
'items.date_start',
'items.date_end',
'items.location_name',
'items.category_id'
)
->join('items', 'items.item_id', '=', 'orders.item_id')
->leftJoin('orders_items', function ($join) use ($user_id)
{
$join->on('orders_items.order_id', '=', 'orders.order_id')
->on('orders_items.item_id', '=', 'orders.item_id')
->where('orders_items.user_id', '=', $user_id);
})
->with('Items')
->where(function ($query) use ($select_balance) { // This is where event is in the future, or it has a balance and is not draft.
$query->where('items.date_end', '>=', Carbon\Carbon::now()->toDateString())
->orWhere(function ($query) use ($select_balance) {
$query->where(DB::raw($select_balance), '>', 0)
->whereNotIn('orders.status', [0, 79]);
});
});
i would like to set a condition if (date_start == date_end) then
$query->where('items.date_end', '>=', Carbon\Carbon::now()->toDateString())
->orWhere(function ($query) use ($select_balance) {
$query->where(DB::raw($select_balance), '>', 0)
->whereNotIn('orders.status', [0, 79]);
});
else
$query->where('items.date_end', '>', Carbon\Carbon::now()->toDateString())
->orWhere(function ($query) use ($select_balance) {
$query->where(DB::raw($select_balance), '>', 0)
->whereNotIn('orders.status', [0, 79]);
});
how to set a condition within query i try to end query after ->with(items) but that say $q-> has syntax error please guide and explain how to over come this issue
You can use conditional clauses: https://laravel.com/docs/5.5/queries#conditional-clauses
Animals::when($condition, function($query) {
$query->where('size', 'big');
})->when(!$condition, function($query)) {
$query->where('size', 'small');
})->get();
In the example, if $condition is true then we fetch big animals, otherwise we fetch small animals.
Related
I have this query
$t_items_between = TransferItem::whereHas('transfer', function ($query) use($old_until_date, $inventory_id, $old_from_date_interval) {
$query->where('document_date', '>=', $old_from_date_interval);
$query->where('document_date', '<=', $old_until_date);
$query->where('to_inventory_id', $inventory_id);
})
->with(['transfer' => function($query) use($old_until_date, $inventory_id, $old_from_date_interval) {
$query->where('document_date', '>=', $old_from_date_interval);
$query->where('document_date', '<=', $old_until_date);
$query->where('to_inventory_id', $inventory_id);
}
])
->whereIn('item_id', $subset)
->addSelect(['quantity' => TransferItem::selectRaw('sum(quantity)')
->groupBy('item_stock_id')
->from((new TransferItem)->getTable() . ' as ti')
->whereColumn('ti.item_stock_id', (new TransferItem)->getTable() . '.item_stock_id')
])
->get();
So TransferItem is the model of the table transfer_items. transfer_items has 4 columns: id, transfer_id, item_stock_id and quantity.
What I am trying to do is to sum the quantity of each result, grouping by the item_stock_id, but the query above doesn't work. How would I approach it?
try using map
$t_items_between = TransferItem::whereHas('transfer', function ($query) use($old_until_date, $inventory_id, $old_from_date_interval) {
$query->where('document_date', '>=', $old_from_date_interval);
$query->where('document_date', '<=', $old_until_date); $query->where('to_inventory_id', $inventory_id);
})
->with(['transfer' => function($query) use($old_until_date, $inventory_id, $old_from_date_interval) {
$query->where('document_date', '>=', $old_from_date_interval);
$query->where('document_date', '<=', $old_until_date); $query->where('to_inventory_id', $inventory_id);
}
])
->whereIn('item_id', $subset)
->get()
->groupBy('item_stock_id')
->map(function ($q) {
return $q->sum('quantity');
});
Visit https://laravel.com/docs/9.x/collections
I am struggling to add a whereIn to a more complex query:
$events = Toolplan::query()
->select('id', 'eventId', 'resourceId', 'title', 'start', 'end')
->where(function ($query) use ($start, $end) {
$query->whereDate('start', '>=', $start)->whereDate('end', '<=', $end);
})
->orWhere(function ($query) use ($start, $end) {
$query->whereDate('start', '<=', $start)->whereDate('end', '>=', $end);
})
->orWhere(function ($query) use ($start, $end) {
$query->whereDate('end', '>=', $start)->whereDate('end', '<=', $end);
})
->orWhere(function ($query) use ($start, $end) {
$query->whereDate('start', '>=', $start)->whereDate('start', '<=', $end);
})
// gives "General error: 1096 No tables used":
->whereIn('eventId', function ($query) use ($eventIds){
$query->whereIn('eventId', $eventIds);
})
// no effect:
// ->whereIn('eventId', $eventIds)
->get();
I tried to add it as a function and in simple form but the function leads to General error: 1096 No tables used and the other shows no effect at all, because the collection is far too large and should be only 4 items after whereIn and the orWhere selection. $eventIds is a flat array:
["P000018","P000054","P000021","P000030"]
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);
})