Laravel display withCount() query results in groups - laravel

I have a query where I get count totals using withCount().
$applications = Application::with('company')->withCount([
'task',
'task as task_updated_count' => function ($query) {
$query->where('task_status', '!=', 1);
}])->get();
In my view I can loop through and display the results.
<table class="table">
<thead>
<tr>
<th>Applications</th>
<th class="text-center">Tasks</th>
<th class="text-center">Updated Tasks</th>
</tr>
</thead>
<tbody>
#foreach ($applications as $application)
<tr>
<td>{{ $application->company->company_acronym }}</td>
<td>{{ $application->application_name }}</td>
<td class="text-center">{{ $application->task_count }}</td>
<td class="text-center">{{ $application->task_updated_count }}</td>
</tr>
#endforeach
</tbody>
</table>
This will obviously give me a table listing the results with the $application->company->company_acronym listed for each $application.
What I'm trying to do list the applications by company "$application->company->company_acronym".
So the result would be:
Company 1 Acronym
- AppName Count
- AppName Count
- AppName Count
Company 2 Acronym
- AppName Count
- AppName Count
- AppName Count
Company 3 Acronym
- AppName Count
- AppName Count
- AppName Count

Use groupBy function in collections to group by the acronym.
$applications = Application::with('company')->withCount([
'task',
'task as task_updated_count' => function ($query) {
$query->where('task_status', '!=', 1);
}])->get();
$applicationGroups = $applications->groupBy(function($application) {
return $application->company->company_acronym;
});
Then you can iterate over the groups to get desired output.
<table class="table">
<thead>
<tr>
<th>Applications</th>
<th class="text-center">Tasks</th>
<th class="text-center">Updated Tasks</th>
</tr>
</thead>
<tbody>
#foreach ($applicationGroups as $group => $applications)
<tr>
<td colspan="3">{{ $group }}</td>
</tr>
#foreach ($applications as $application)
<tr>
<td>{{ $application->application_name }}</td>
<td class="text-center">{{ $application->task_count }}</td>
<td class="text-center">{{ $application->task_updated_count }}</td>
</tr>
#endforeach
#endforeach
</tbody>
</table>

Please Check Following Code
$applications = Application::with('company')->with([
'task' => function ($query) {
$query->where('task_status', '!=', 1)->count();
},
'task_updated_count' => function ($query) {
$query->where('task_status', '!=', 1)->count();
}])->get();

Related

How to sum table row data in laravel 8

I want to sum up the data in my table. Please tell, How can I do it. I have tried the below method but it is not working.
This is my controller code
public function CreateRentCertificateReport(Request $request)
{
$data['reports'] = Report::distric()->status(1)->get();
return view('adc.reports.start-create-report', $data);
}
This is my view code
<tbody>
#foreach($reports as $data)
<tr>
<td>{{$data->upazila->upazila_name}}</td>
<td class="numeric_bangla">{{$data->column_one}}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
#foreach($reports as $data)
<td>{{$data['column_one']->countBy()}}</td>
#endforeach
</tr>
</tbody>
You can use pluck() and sum() :
<tbody>
#foreach($reports as $data)
<tr>
<td>{{ $data->upazila->upazila_name }}</td>
<td class="numeric_bangla">{{ $data->column_one }}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
<td>{{ $reports->pluck('column_one')->sum() }}</td>
</tr>
</tbody>
Maybe by doing something like this:
#php
$total = 0;
foreach($reports as $report) {
$total += $report['column_one']
}
#endphp
<tbody>
#foreach($reports as $report)
<tr>
<td>{{$report->upazila->upazila_name}}</td>
<td class="numeric_bangla">{{$report->column_one}}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
<td>{{$total}}</td>
</tr>
</tbody>
Instead of calling ->get() on an Eloquent query, you can call ->sum(“column_name”) to get a number which is the sum of that column’s values

Cannot use object of type stdClass as array (View:

I want to use data 2 query in 1 view but
Error Cannot use object of type stdClass as array
(View:
public function adtranspc(Request $request)
{
$writter = Writter::all();
$processmaster = DB::table('rocessmaster')
->where('pcm_bname', 'LIKE', "%Social%")
->get();
return view('processspj.adtranspc',[
'writter' => $writter,
'processmaster' => $processmaster
]};
*This is my view (this error here)
<table id="table2" class="table table-hover table-striped">
<tr>
<th scope="col"></th>
<th scope="col">Colname1</th>
</tr>
#foreach ($processmaster as $item)
<tr>
<td>{{ $item['pcm_id'] }}</td>
<td>{{ $item['pcm_bname'] }}</td>
</tr>
#endforeach
</table>
This is my controller
public function adtranspc(Request $request)
{
$writter = Writter::all();
$processmaster = DB::table('rocessmaster')
->where('pcm_bname', 'LIKE', "%Social%")
->get();
return view('processspj.adtranspc',[
'writter' => $writter,
'processmaster' => $processmaster
]};
}
You are trying to print your object values as an array.You need to update your code with this, access your object values like this way
#foreach ($processmaster as $item)
<tr>
<td>{{ $item->pcm_id }}</td>
<td>{{ $item->pcm_bname }}</td>
</tr>
#endforeac

Nested Looping in Laravel Blade Templating

Here is my Controller
When I run this controller code I get the result as in the screenshot below, as all data is correct.
$query = DB::table('intis_auctions_lots')
->select('bidincrement','intis_lot_id','lot','product_name','maxbid','enddate')
->where([['community_name','=',$CommunityNo],['SellerId','=',$SellerId],['active','=','Y'],['startdate','<',$now],['enddate','>',$now]])
->orderBy('intis_lot_id')
->get();
foreach($query as $querys) {
$qintis_lot_id = $querys->intis_lot_id;
$qenddate = date('H:i', strtotime($querys->enddate));
$individualbids = DB::table('intis_auctions_lots')
->select(DB::raw('sum(nrbids) as nbid,lot, startprice,intis_lot_id,product_name,enddate,maxbid'))
->where([['community_name','=',$CommunityNo],['SellerId','=',$SellerId],['active','=','Y'],['startdate','<',$now],['enddate','>',$now],['intis_lot_id','=',$qintis_lot_id]])
->groupBy('startprice','intis_lot_id','product_name','lot','enddate','maxbid')
->get();
foreach($individualbids as $key => $value)
echo "<tr><td>".$value->lot."</td>
<td>".$value->intis_lot_id."</td>
<td>".$value->product_name."</td>
<td>".date('H:i', strtotime($value->enddate))."</td>
<td>".$value->startprice."</td>
<td>".$value->maxbid."</td>
</tr>";
}
}
My View code as below
<div class="table-responsive">
<table id="zero_config" class="table table-striped table-bordered">
<thead>
<tr>
<th>Lot</th>
<th>Product Name</th>
<th>End Time</th>
<th>Bids</th>
<th>Start Price</th>
<th>Current Bid Amount</th>
<th>Name</th>
</tr>
</thead>
<tbody>
#foreach ($query as $key=>$value)
#foreach ($individualbids as $key => $value1)
<tr>
<td>{{ $value->lot }}</td>
<td style="text-align:left">{{ $value->intis_lot_id }},{{ $value->product_name }}</td>
<td>{{ date('H:i', strtotime($value->enddate)) }}</td>
<td></td>
<td>{{ $value1->startprice }}</td>
<td>{{ $value->maxbid }}</td>
<td></td>
<td></td>
</tr>
#endforeach
#endforeach
</tbody>
<tfoot>
<tr>
</tr>
</tfoot>
</table>
</div>
When pass it to the view I'm getting result as in the screen below, as the Starting Price as 340 it takes the first value to all the row as same value, but when I run in the controller code without passing to view it show correct value. Please provide me the solution to write the proper code in the view, as it will helpful to me.

Paginating in Laravel. Why show only 15 results?

Why I obtain just 15 results? We have more than 20000 entries.
My Controller:
namespace GBCustomer\Http\Controllers;
use GBCustomer\Customer;
use Illuminate\Http\Request;
class CustomerController extends Controller
{
public function listCustomers()
{
$customers = DB::table('customers')->paginate(10);
return view('customer.index',['customers' => $customers]);
}
}
My route
Route::get('/customer/list', 'CustomerController#listCustomers');
My view (part)
<table id="datatable-buttons" class="table table-striped table-bordered"
cellspacing="0" width="100%">
<thead>
<tr>
<th>ID Customer</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
<tbody>
#foreach( $customers as $customer )
<tr role="row">
<td> <a href="customer/show/{{ $customer->id_customer }}">
{{ $customer->id_customer }} </a></td>
<td>{{ $customer->firstname }}</td>
<td>{{ $customer->lastname }}</td>
<td>{{ $customer->email }}</td>
</tr>
#endforeach
</tbody>
</table>
If I put DB::table('customers')->paginate(1500); show 1500 results paginated in 15 results.
If I put DB::table('customers')->paginate(2000); show 2000 results paginated in 15 results.
If I put DB::table('customers')->paginate(); show 15 results paginated in 15 results.
If I change this line for:
$customers = Customer::all(); show all results (about 22000) but in one page!
?¿?¿
Thanks in advance

Laravel 5 unable to render pagination

I'm new to Laravel 5 and I have an issue displaying pagination. I looked in the documentation and just can't get it to work.
usercontroller:
public function getIndex()
{
$users = User::where('active', '=','verified')->simplePaginate(10);
return View::make('User.index')->with('users',$users);
}
index.blade.php:
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>id</th>
<th>username</th>
<th>email</th>
<th>role</th>
<th>created</th>
<th class="actions">Actions</th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->username }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->role }}</td>
<td>{{ $user->created_at }}</td>
<td class="actions">
blabla
</td>
</tr>
#endforeach
</tbody>
</table>
{!! $users->render() !!}
for some reason, it was printing an additional /
fix:
{!! str_replace('users/','users',$users->render()) !!}

Resources