Count Data In Laravel - php

I want to count data based on status in Laravel 5.7. I have six status (Assigned, On The Way, Started, Cancel Worker, Cancel Admin and Closed). I have to make a query for this inside foreach data.
This is my code:
$data['list_detail'] = [];
foreach ($listFa as $value) {
$type = $value->type;
$descr = $value->descr;
$c_ass_detail = transaction::where('type', $value->type)
->where('status','like','%Assigned%')
->groupBy('type')
->count();
$c_otw_detail = transaction::where('type', $value->type)
->where('assign_status','like','%On The Way%')
->groupBy('type')
->count();
$c_str_detail = transaction::where('type', $value->type)
->where('assign_status','like','%Started%')
->groupBy('type')
->count();
$c_cbw_detail = transaction::where('type', $value->type)
->where('assign_status','like','%Cancel Worker%')
->groupBy('type')
->count();
$c_cba_detail = transaction::where('type', $value->type)
->where('assign_status','like','%Cancel Admin%')
->groupBy('type')
->count();
$c_cls_detail = transaction::where('type', $value->type)
->where('assign_status','like','%Closed%')
->groupBy('type')
->count();
$total = $c_ass_detail + $c_otw_detail + $c_str_detail + $c_cbw_detail + $c_cba_detail + $c_cls_detail;
array_push($data['list_detail'], array('type'=>$type, 'descr'=>$descr, 'c_ass_detail'=>$c_ass_detail, 'c_otw_detail'=>$c_otw_detail, 'c_str_detail'=>$c_str_detail, 'c_cbw_detail'=>$c_cbw_detail, 'c_cba_detail'=>$c_cba_detail, 'c_cls_detail'=>$c_cls_detail, 'total'=>$total));
}
and this is my view code:
<tbody>
#foreach($list_detail_fa as $row)
<tr class="tr_dashboard">
<td>{{ $row['fa_type_cd'] }}</td>
<td>{{ $row['descr'] }}</td>
<td>{{ $row['c_ass_detail'] }}</td>
<td>{{ $row['c_otw_detail'] }}</td>
<td>{{ $row['c_str_detail'] }}</td>
<td>{{ $row['c_cbw_detail'] }}</td>
<td>{{ $row['c_cba_detail'] }}</td>
<td>{{ $row['c_cls_detail'] }}</td>
<td>{{ $row['total'] }}</td>
</tr>
#endforeach
</tbody>
It's working, but I think this way is not good because when I refresh the page, it's so slow to show back the data. Maybe it's because to many same queries. So how to good way, if I want to count the data just using one query for each status

You need to use aggregate function count within your query with group by. Assuming your table name is transactions. Try this!
$typeCounts = DB::table('transactions')
->select('type', 'assign_status', DB::raw('count(type) as type_count'))
->where('type', $value->type)
->groupBy('assign_status')
->get();
And then in twig you can access it like this:
<tbody>
<tr class="tr_dashboard">
#foreach($typeCounts as $row)
<td>{{ $row['type_count'] }}</td>
#endforeach
</tr>
</tbody>

You're over-complicating things for yourself:
You simply need:
$allTypes = array_map(function ($value) {
return $value->type;
}, $listFa);
$c_ass_detail = transaction::whereIn('type', $allTypes)
->groupBy('type')
->groupBy('assign_status')
->select('type', 'assign_status', \DB::raw('count(*) as count'))
->get();
Then your $c_ass_detail will have a row for each type, assign status, count combination.

Related

display different data with the same foreign key then the data is displayed in 1 row (laravel 5.7)

I want to display data with the same foreign key but different contents in a 1-row table view in Laravel, can anyone help me?
Current table view
Table view I want
ProductController
public function export($id)
{
$tgl = Product_Keluar::where('periode_id', $id)->value('tanggal');
$dtt = carbon::parse($tgl)->daysInMonth;
$month = carbon::parse($tgl)->format('M-Y');
$data2 = Product_Keluar::join('drug', 'product_keluar.drug_id', '=', 'drug.id')->join('periode', 'product_keluar.periode_id', '=', 'periode.id')->select('product_keluar.drug_id', 'product_keluar.qty', 'product_keluar.tanggal', 'periode.periode', 'drug.nama_obat', 'drug.satuan')->orderBy('drug_id', 'ASC')
->orderBy('tanggal', 'ASC')
->where('periode_id', $id)
->get()->groupBy('drug_id');
return view('product_keluar.productKeluarAllExcel', compact('tgl', 'dtt', 'month', 'data2'));
}
Blade
#foreach($data2 as $key => $items)
#foreach ($items as $item)
<tbody>
<tr>
<td>{{ ++$key }}</td>
<td>{{$item->nama_obat}}</td>
<td>{{$item->satuan}}</td>
<td>{{$item->satuan}}</td>
<td>{{$item->periode}}</td>
#for ($i = 1; $i <= $dtt; $i++)
#if(date('d', strtotime($item->tanggal)) == $i && !empty($item->qty))
<td>{{ $item->qty }}</td>
#else
<td>-</td>
#endif
#endfor
</tr>
</tbody>
#endforeach
#endforeach

If there are multiple expenses in a project, how can I sum them together and make a single row?

I have two table as you can see that relations each other.
I want to see this table
How to do that in my blade file or controller.
this is my blade file;
#foreach ($costs as $cost)
#foreach ($projects as $project)
#if($project->id == $cost->project_id)
<tr class="odd">
<td>{{ $project->id }}</td>
<td>{{ $cost->price }}</td>
</tr>
#endif
#endforeach
#endforeach
public function tablo1()
{
$costs = DB::table('cost_manages')
->whereExists(function ($query) {
$query->select(DB::raw(1))
->from('projects')
->whereColumn('projects.id', 'cost_manages.project_id')->groupBy('project_id');
})
->get();
$projects = DB::table('projects')
->whereExists(function ($query) {
$query->select(DB::raw(1))
->from('cost_manages')
->whereColumn('cost_manages.project_id', 'projects.id');
})
->get();
return view('home.muhasebe_Tablo1', [
'costs' => $costs,
'projects' => $projects
]);
}
That one is my controller.
There are 2 costs for project number 11. I want to write a single record by collecting them.
Please help me
Use join:
DB::table('projects')
->leftjoin('cost_manages', 'projects.id','const_manages.project_id')
->select('projects.id as id', 'projects.name as name', 'cost_manages.cost as cost')->get()
and then loop through this.

Undefined property: stdClass - LARAVEL

I'm trying to make an admin page where the owner of the store can check the info from users that singed on the website, but I always get the Undefined property: stdClass::$name error
Here's the controller function:
public function listar(){
$users = DB::table('users')->select('name')->orderBy('updated_at', 'desc')->get();
$users = DB::table('users')->select('email')->orderBy('updated_at', 'desc')->get();
$users = DB::table('users')->select('phone')->orderBy('updated_at', 'desc')->get();
}
Here's part of the form:
#foreach ($users as $u)
<tr>
<td>{{ $u->name }}</td>
<td>{{ $u->email }}</td>
<td>{{ $u->phone}}</td>
</tr>
#endforeach
I just want it to be able to show this info from the database.
You're actually running the query three separate times, overwriting the $users variable each time. You end up with the last one, and it doesn't have a name property because you only selected phone. You should run it just once and specify all the columns you want in select().
public function listar(){
$users = DB::table('users')->select('name', 'email', 'phone')
->orderBy('updated_at', 'desc')->get();
}
public function listar(){
$users = //Your user model class ::all();
return $users;
}
Then try to view
#foreach ($users as $u)
<tr>
<td>{{ $u->name }}</td>
<td>{{ $u->email }}</td>
<td>{{ $u->phone}}</td>
</tr>
#endforeach

Laravel - Select data to Display in Table

I want to ask if ever I can limit my data in table. I want to remove one data out of my 6 data.
here is my table
Now I want to remove number 3 row which is SESSION_VALIDITY how can I remove that data and make it just 5 data in my table? I want to limit my data coz' I dont need to view SESSION_VALIDITY. help thanks
my Index
#foreach ($settings as $setting)
<tr>
<td>{{ $setting->settings_code }}</td>
<td>{{ $setting->subject}}</td>
<td>{{ $setting->description }}</td>
<td></td>
</tr>
#endforeach
my Controller
public function index()
{
$settings = Setting::all();
return view('admin.settings.index', compact('settings'));
}
If you just want to not show that row in your $settings data, you can exclude it using your query like this.
public function index()
{
$settings = Setting::where('settings_code', '<>', 'SESSION_VALIDITY')->get();
return view('admin.settings.index', compact('settings'));
}
If you want to delete it entirely, you can run a one-time function to delete that row from the table.
public function deleteSessionValidity()
{
$setting = Setting::where('settings_code', 'SESSION_VALIDITY')->first();
$setting->delete();
}
You should filter your query in the controller.
Instead of Setting::all(), you should use:
$settings = Setting::where('settings_code', '!=', 'SESSION_VALIDITY')->get();
You should try this:
public function index()
{
$settings = Setting::where('settings_code', '!=', 'SESSION_VALIDITY')->get();
return view('admin.settings.index', compact('settings'));
}
OR
#foreach ($settings as $setting)
<tr>
#if($setting->settings_code != "SESSION_VALIDITY")
<td>{{ $setting->settings_code }}</td>
<td>{{ $setting->subject}}</td>
<td>{{ $setting->description }}</td>
<td></td>
#endif
</tr>
#endforeach
inside foreach put if statement
like:
#foreach ($settings as $setting)
#if ($setting->settings_code != "SESSION_VALIDITY")
your <td> here
#endif
#endforeach

calling data from database from controller to view using laravel

Controller:
public function index() {
$data = DB::table('tusers','tbills','tpackages')
->join('tbills', 'tusers.user_id', '=', 'tbills.user_id')
->join('tpackages', 'tbills.package_id', '=', 'tpackages.package_id')
->select('tusers.user_id','tusers.company_name','tusers.first_name', 'tbills.account_no', 'tpackages.package_name','tbills.start_date','tbills.end_date','tbills.bill_status')
->get();
return View::make('account')->with('data',$data);
}
`
view:
#forelse($data as $value)
<td>{{ $value->user_id }}</td>
<td>{{ $value->company_name }}</td>
<td>{{ $value->first_name }}</td>
<td>{{ $value->account_no }}</td>
<td>{{ $value->package_name }}</td>
<td>{{ $value->start_date }}</td>
<td>{{ $value->end_date }}</td>
<td>{{ $value->bill_status }}
I want to call $data from packagecontroller to account.blade but there is an error
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "tusers" does not exist
LINE 1: ... "tbills"."end_date", "tbills"."bill_status" from "tusers" i...
^ (SQL: select "tusers"."user_id", "tusers"."company_name", "tusers"."first_name", "tbills"."account_no", "tpackages"."package_name", "tbills"."start_date", "tbills"."end_date", "tbills"."bill_status" from "tusers" inner join "tbills" on "tusers"."user_id" = "tbills"."user_id" inner join "tpackages" on "tbills"."package_id" = "tpackages"."package_id")
Thanks for your help.
Remove the other 2 table names from table(). Something like this.
public function index() {
$data = DB::table('users','bills','packages')
->join('bills', 'users.id', '=', 'bills.user_id')
->join('packages', 'bills.package_id', '=', 'packages.id')
->select('users.id','users.company_name','users.first_name', 'bills.account_no', 'packages.package_name', 'bills.start_date', 'bills.end_date', 'bills.bill_status')
->get();
return view('account', ['data' => $data]);
}
Mind column naming please. I may have changed some names, so you have to make sure you have the correct names.
In your view do
#foreach( $data as $value )
<td>{{ $value->company_name }}</td>
...
#endforeach
It seems you are skipping some steps. Learn properly through this free video series how to work with Laravel: https://laracasts.com/series/laravel-from-scratch-2017

Categories