I have this function that help me to find sum of two rows and get output of total cost So again I want to get sum of the output(total cost).
public function getUsageData(Request $request)
{
$start_date = $request->get('start_date');
$end_date = $request->get('end_date');
$particulars =DB::table('particulars')
->join('reqs', 'particulars.particular_id', "=", 'reqs.particular_id')
->whereBetween('date_issued', [$start_date, $end_date])
->select('particulars.item_name','particulars.unit','particulars.price','reqs.quantity_issued',
DB::raw('sum(particulars.price*reqs.quantity_issued)AS total_cost'))
->groupBy('particulars.particular_id')
->get();
}
I get sum of two row that form Total cost. I want to get sum of total cost column at the bottom so as to make it easy to understand for user without calculating it.
Try with this code else share the table structure please
$particulars =DB::table('particulars as A')
->leftjoin('reqs as B', function ($join) {
$join->on('A.particular_id', '=', 'B.particular_id');
})
->whereBetween('A.date_issued', [$start_date, $end_date]) // i have taken date issued from table particulars as you have not mentioned else you can change A.date_issued as B.date_issued .
->select('A.item_name','A.unit','A.price','B.quantity_issued',DB::raw('sum(A.price*B.quantity_issued) as total_cost'))
->groupBy('A.particular_id')
->get();
Related
i have 2 tables
invoice (id, service_inv_cat_id, etc..)
payment (id, invoice_id, amount)
I need to get the sum of amount in payment with groupby (service_inv_cat_id)
here is my code
$income_details_cat = Invoice::select('id', 'service_inv_cat_id')
->with(['service_inv_cat' => function ($q) {
$q->select('id', 'name');}])
->with(['payment' => function ($q) use ($year, $month){
$q->select('id','invoice_id', 'amount')
->whereYear('paid_date', $year)
->whereMonth('paid_date', $month)
;}])
->whereHas('payment', function ($q) use ($year,$month) {
return $q->where('type', 3)
->whereYear('paid_date', $year)
->whereMonth('paid_date', $month)
;})
->withSum(['payment' => function ($query) use ($year, $month){
$query->whereYear('paid_date', $year)
->whereMonth('paid_date', $month);
}], 'amount')
->get();
it returns the following
but I need to get the total sum of wallet,
I have tried added ->groupBy('service_inv_cat_id')
but it returns the following
it returns the sum of payment of only one record, ex wallet has higher than 200 in total with many invoices as you can see in pic 1 but its returns only one
I assume service_cat and payment belongTo payment ?
$income_details_cat = Invoice::select('invoices.id', 'invoices.service_inv_cat_id', 'payments.amount')
->join('payments', 'invoices.id', '=', 'payments.invoice_id')
->with('service_inv_cat:id,name')
->sum('payments.amount')
->where('payments.type', 3)
->whereYear('payments.paid_date', $year)
->whereMonth('payments.paid_date', $month)
->groupBy('service_inv_cat_id')
->get();
I am a bit lost to be honest but I would re-write your code like this not sure why you have so many withs for the same thing I think you would get the same result as you have now if written like this:
$income_details_cat = Invoice::select('id', 'service_inv_cat_id')
->with('service_inv_cat:id,name', 'payment:id,invoice_id,amount')
->wherHas(['payment' => fn ($q) =>
$q->where('type', 3)
->whereYear('paid_date', $year)
->whereMonth('paid_date', $month)
])
->get();
if you can show your relationships a bit clearer and explain what laser is ? I take it wallet is the sum of all amounts grouped by service_inv_cat_id
Right now I have a subquery to get the count of payments for the current month and then getting the 4 products with the highest payment_count. This query works fine but I'm wondering if there's a more simple way to do the same since its getting difficult to read.
$latestPayments = DB::table('payments')
->select('product_id', DB::raw('COUNT(*) as payments_count'))
->whereMonth('created_at', Carbon::now()->month)
->groupBy('product_id');
$trendingProducts = DB::table('products')
->joinSub($latestPayments, 'latest_payments', function ($join) {
$join->on('products.id', '=', 'latest_payments.product_id');
})->orderBy('payments_count', 'DESC')->take(4)->get();
This did it!
$trendingProducts = Product::withCount(['payments' => function($query) {
$query->whereMonth('created_at', Carbon::now()->month);
}])->orderBy('payments_count', 'DESC')->take(4)->get();
If you are using eloquent query with relational database you can do like this:
$latestPaymentWithTrendingProduct = App\Payment::with(['products', function($product) {
$product->orderBy('payments_count', 'DESC')->take(4);
}])->whereMonth('created_at', date('m'))->get()->groupBy('product_id');
This will lessen the code but still do the same thing.
I need to sum every paid_amount inside of each expense_payment_liab instead of returning all these
individual paid_amount as it can be seen in images for clear view.So far my query looks like this:
Model Relation is like Voucher has many Expenses and Expenses has many Expensepaymentliab.
This is so far what I tried:
$expense = Voucher::where('voucher_date', $request->date)
->where('account_id', 1)
->with(['expenses' => function ($expense) use ($date) {
$expense->with(['expPaymentLiab' => function ($inner) use ($date) {
return $inner->select('paid_amount', 'expense_id')->where('paid_date', '=', $date);
//need to return the sum of paid_amount.
// I've also tried this
//$inner->sum('paid_amount')
// $inner->sum('paid_amount', 'expense_id')
}]);
$expense->select('id', 'date', 'total_amount', 'voucher_id');
}])->get();
These are the images please check
Need to sum such paid_amount field.
You can use withCount to sum the paid_amount field.
Voucher::where('voucher_date', $request->date)
->where('account_id', 1)
->with(['expenses' => function ($expense) use ($date) {
// select the columns first, so the subquery column can be added later.
$expense->select('id', 'date', 'total_amount', 'voucher_id');
$expense->withCount(['expPaymentLiab AS paid_sum' => function ($query) use ($date) {
return $query->select(DB::raw('SUM(paid_amount)'))->where('paid_date', '=', $date);
}]);
}])
->get();
I'm having trouble retrieving data from the database using Eloquent. I am trying to retrieve all data between two dates (created_at field).
I've done this:
public function search(Request $request){
$start_date = $request->get('start_date');
$end_date = $request->get('end_date');
$getinfo = DB::table('media')
->join('content_forms', 'media.id', "=", 'content_forms.media_id')
->whereBetween('Date_Occured',[$start_date, $end_date])
->get();
dd($getinfo);
}
if you want to select between dates of the media table for example, then fully qualify the field e.g.
$getinfo = DB::table('media')
->join('content_forms', 'media.id', "=", 'content_forms.media_id')
->whereBetween('media.created_at', [$start_date, $end_date]);
->get();
Also make sure the dates passed in the request are in a correct YYYY-mm-dd format
I'm trying to redesign my Laravel 4.2 code and like to group a list from results over the last days.
code:
public function getTrending($type = null, $category = null)
{
$posts = $this->posts($type, $category)->with('comments', 'votes', 'category', 'user', 'votes.user')
->leftJoin('votes', 'posts.id', '=', 'votes.post_id')
->leftJoin('comments', 'posts.id', '=', 'comments.post_id')
->select('posts.*', DB::raw('count(comments.post_id)*7 + count(votes.post_id)*30 + posts.views as popular'))
->groupBy('posts.id')->with('user')->orderBy('popular', 'desc')->whereMonth('posts.created_at', '>=', date('n', strtotime('-1 month')))
->paginate(perPage());
return $posts;
}
I want to group the results as it is (relevance: comments, votes, visit) + grouping the results on a daily base.
Like:
Today (with Date xx.xx.xx)
result 1 (Max Votes, Comment, ...)
result 2
result 3
Yesterday (with Date xx.xx.xx)
result 4 (Max Votes, Comments, ...)
result 5
result 6
Is this possible?
You can use Carbon;
->groupBy(function($date) {
return Carbon::parse($date->created_at)->format('Y'); // grouping by years
//return Carbon::parse($date->created_at)->format('m'); // grouping by months
});
No I don't believe you can group your data in a single query like that.
Assuming your code is giving you the dataset you desire, without grouping by date, I would loop over the dates I want and get the data for every day and format it with the date heading etc.