collect
write eager loader relations ships
Laravel tip. In Eloquent, you can combine `whereHas()` and `orDoesntHave()` in one sentence. Doesn't it read like a proper natural English language sentence? Beautiful code.
open apis
Laravel tip.
Extra Filter Query on Relationships.
If you want to load relationship data, you can specify some limitations or ordering in a closure function. For example, if you want to get Countries with their biggest cities, here's the code.
Order::where(['checked' => 0])->update(['checked' => 1]);
$orders = Order::with(['customer', 'seller.shop'])
->when($status != 'all', function ($q) use($status){
$q->where(function ($query) use ($status) {
$query->orWhere('order_status', $status);
});
})
->when($filter,function($q) use($filter){
$q->when($filter == 'all', function($q){
return $q;
})
->when($filter == 'POS', function ($q){
$q->whereHas('details', function ($q){
$q->where('order_type', 'POS');
});
})
->when($filter == 'admin' || $filter == 'seller', function($q) use($filter){
$q->whereHas('details', function ($query) use ($filter){
$query->whereHas('product', function ($query) use ($filter){
$query->where('added_by', $filter);
});
});
});
})
->when($request->has('search') && $search!=null,function ($q) use ($key) {
$q->where(function($qq) use ($key){
foreach ($key as $value) {
$qq->where('id', 'like', "%{$value}%")
->orWhere('order_status', 'like', "%{$value}%")
->orWhere('transaction_ref', 'like', "%{$value}%");
}});
})
->when($request->has('date_type')&& $request->date_type == "this_year", function($dateQuery) {
$current_start_year = date('Y-01-01');
$current_end_year = date('Y-12-31');
$dateQuery->whereDate('created_at', '>=',$current_start_year)
->whereDate('created_at', '<=',$current_end_year);
})
->when($request->has('date_type')&& $request->date_type == "this_month", function($dateQuery) {
$current_month_start = date('Y-m-01');
$current_month_end = date('Y-m-t');
$dateQuery->whereDate('created_at', '>=',$current_month_start)
->whereDate('created_at', '<=',$current_month_end);
})
->when($request->has('date_type')&& $request->date_type == "this_week", function($dateQuery) {
$start_week = Carbon::now()->subDays(7)->startOfWeek()->format('Y-m-d');
$end_week =Carbon::now()->startOfWeek()->format('Y-m-d');
$dateQuery->whereDate('created_at', '>=',$start_week)
->whereDate('created_at', '<=',$end_week );
})
->when($request->has('date_type')&& $request->date_type == "custom_date" && !empty($from) && !empty($to), function($dateQuery) use($from, $to) {
$dateQuery->whereDate('created_at', '>=',$from)
->whereDate('created_at', '<=',$to);
})
->when($delivery_man_id, function ($q) use($delivery_man_id){
$q->where(['delivery_man_id'=> $delivery_man_id]);
})
->when($request->customer_id != 'all' && $request->has('customer_id') ,function($query)use($request){
return $query->where('customer_id',$request->customer_id);
})
->when($request->seller_id != 'all' && $request->has('seller_id') && $request->seller_id != 0 ,function($query)use($request){
return $query->where(['seller_is'=>'seller','seller_id'=>$request->seller_id]);
})
->when($request->seller_id != 'all' && $request->has('seller_id') && $request->seller_id == 0 ,function($query)use($request){
return $query->where(['seller_is'=>'admin']);
})
->latest('id')
->paginate(Helpers::pagination_limit())
->appends([
'search'=>$request['search'],
'filter'=>$request['filter'],'from'=>$request['from'],
'to'=>$request['to'],
'date_type' =>$request['date_type'],
'customer_id'=> $request->customer_id,
'seller_id' => $request->seller_id,
'delivery_man_id'=>$request['delivery_man_id'],
]);
==
No comments:
Post a Comment