How to display count result to blade template - php

I have attendance table then I perform count to total all the Present(P)Absent(A)Late(L) now how can I show it to blade template inside the html input with 3 separated value example: value=P, value=A, value=L;
right now with my code I'm getting it as all
controller:
public function get_status(){
$from = date('2018-01-01');
$to = date('2018-03-31');
$atnds = DB::table('attendances')
->select(DB::raw('count(*) as total, status'))
->where('status', '<>', 'status')
->whereBetween('days', [$from,$to])
->groupBy('status')
->where('lead_id', '=', 1)
->get();
return view('total',compact('atnds'));}
DD RESULT
[{"total":7,"status":"A"},{"total":9,"status":"L"},{"total":65,"status":"P"}]

If you want with input then you can use it like this
<table style="width:100%">
<tr><th>Status </th> <th>Total</th></tr>
#foreach($atnds as $at)
<tr>
<td>{{ $at->status }}</td>
<td><input type="text" value ="{{ $at->total }}" /></td>
</tr>
#endforeach
</table>

In blade you could use
<table style="width:100%">
#foreach($atnds as $ad)
<tr>
<td>{{ $ad->total }}</td>
<td>{{ $ad->status }}</td>
</tr>
#endforeach
</table>
If you want to break them out i would do so before returning it to the blade
$status = []
foreach($atnds as $ad) {
$status[$ad->status] => $ad->total
}
$status = collect($status);
return view('total',compact('status'));
and in the blade you should now be able to do
{{ $status->L }}
or
{{ $status['L'] }}

Related

Laravel Join and groupBy Cannot Show Column Data

$result = DB::table('students')
->join('cities', 'cities.id', '=', 'students.city_id' )
->select('cities.city_name as CityStudent', DB::raw('COUNT(students.id) as total'))
->groupBy('cities.id')->get();
view blade :
#foreach($result as $r)
<tr>
<td>{{ $r->CityStudent }}</td>
<td>{{ $r->total }}</td>
</tr>
#endforeach
I want to display the number of cities. And display the name of the city in the data.
Why does the code error and it doesn't work?

How to join two tables in Laravel

I'm trying to join two different tables to display in the foreach loop in my index.blade file
My Controller:
public function index()
{
$users = DB::table('accounts')
->join('users', 'accounts.user_id', '=', 'users.id')
->select('users.accno', 'accounts.*')
->first();
return view('accounts.index', compact('users'));
}
My blade:
#foreach($users as $user)
<tr>
<td>{{ $user->accno }}</td>
<td>{{ $user->balance }}</td>
<td>{{ $user->amt }}</td>
</tr>
#endforeach
My index.blade.php where I'm implementing the controller, the {{user->accno}} is from my user table and the rest is from accounts table
$users = DB::table('accounts')
->join('users', 'accounts.user_id', '=', 'users.id')
->select('users.accno', 'accounts.*')
->first();
In your controller, you are using for first(). So need to use foreach. Just write:
<tr>
<td>{{ $user->accno }}</td>
<td>{{ $user->balance }}</td>
<td>{{ $user->amt }}</td>
</tr>
Or you should use get() instead of first(). But it depends on your data if you want return only one data you should use first(). If you want to return a lot of data you should use get().
Controller:
$users = DB::table('accounts')
->join('users', 'accounts.user_id', '=', 'users.id')
->select('users.accno', 'accounts.*')
->get();
Blade:
#foreach($users as $user)
<tr>
<td>{{ $user->accno }}</td>
<td>{{ $user->balance }}</td>
<td>{{ $user->amt }}</td>
</tr>
#endforeach

Nested Loop in Eloquent in Laravel

I am having trouble with my code. I have two tables Stocks and StockLists however, I want to get all the related stock in the stocks list based on the stock code. But the result is repeated in each stocks....
Here is the link of the image https://www.screencast.com/t/rHdT2gigh
public function index()
{
$stocks = Stock::all();
foreach ($stocks as $stock) {
$stockInfos = DB::table('stocksList')
->where('stockCode', $stock->stockCode)
->take(3)
->get();
}
return view('welcome', compact('stocks', 'stockInfos'));
}
Here is my blade.php
#forelse($stocks as $stock)
<tr align="left">
<th scope="row">{{ $stock->stockCode }}</th>
<td><strong>{{ $stock->stockName }}</strong></td>
<td><strong>
#forelse($stockInfos as $stockInfo)
{{ $stockInfo->stockDate }} : {{ $stockInfo->stockPrice }}
<br>
#empty
<em>No Data</em>
#endforelse
</strong></td>
</tr>
#empty
#endforelse
You are overwritting the $stockInfos variable try with
public function index()
{
$stocks = Stock::all();
$stockInfos = [];
foreach ($stocks as $stock) {
array_push($stockInfos,DB::table('stocksList')
->where('stockCode', $stock->stockCode)
->take(3)
->get());
}
return view('welcome', compact('stocks'))->with('stockInfos',collect($stockInfos));
}
And in your view :
#forelse($stocks as $stock)
<tr align="left">
<th scope="row">{{ $stock->stockCode }}</th>
<td><strong>{{ $stock->stockName }}</strong></td>
<td><strong>
#forelse($stockInfos->where('stockCode',$stock->stockCode) as $stockInfo)
{{ $stockInfo->stockDate }} : {{ $stockInfo->stockPrice }}
<br>
#empty
<em>No Data</em>
#endforelse
</strong></td>
</tr>
#empty
#endforelse
Try this :
$stockInfos = DB::table('Stock')
->join('stocksList','stocksList.stockCode','=','Stock.stockCode')
->get();

Check if no records exist in view laravel

Okay so I've hit a wall for some reason and I can't figure this out. I wan't to check if there are any records in a query and then use an #if in my view to say either "yes, there are records" or "none". Nothing I've tried is working..
What I want:
<div class="col-md-12 col-style">
<h1 class="no-border">YOUR LATEST CONVERSIONS</h1>
#if(something here that lets me test if $transactions is empty)
<table class="table table-striped">
<tr>
<th>#</th>
<th>Type</th>
<th>User</th>
<th>Amount</th>
<th>Value</th>
<th>Result</th>
<th>Result Value</th>
<th>Date</th>
</tr>
#foreach($transactions as $transaction)
<tr>
<td>{{ $transaction->id }}</td>
<td>{{ $transaction->status }}</td>
<td>{{ $transaction->username }}</td>
<td>{{ $transaction->amount }}</td>
<td>{{ $transaction->value }}</td>
<td>{{ number_format($transaction->result, 2) }}</td>
<td>{{ $transaction->result_value }}</td>
<td>{{ $transaction->created_at }}</td>
</tr>
#endforeach
</table>
#else
yo no records
#endif
</div>
My controller:
public function index($username)
{
$user = User::where('username', $username)->firstOrFail();
$id = $user->id;
$transactions = Transactions::where('user_id', '=', $id)->take(5)->get();
return view('user.profile', compact('user', '=', 'userCurrency', 'transactions'));
So if you see in the top part there's a basic #if. Maybe I'm over thinking it, or am I passing in something that cant be checked the way I want it to?
Thanks!
#if (!$transactions->isEmpty())
...
#endif
this use laravel collection method do the trick.
it is as easy as:
#if(count($transactions) > 0)
#if($transactions) should do the trick.

How to do paginate in Laravel?

so what i want is to use paginate in my view but i got always an error Call to a member function paginate() on a non-object, this is my code :
First my Action :
public function index()
{
$logs = DB::table('services')
->join('logs', function($join)
{
$join->on('services.id', '=', 'logs.service_id');
})
->get()->paginate(10); //here i got the error
return View::make('logs.index',array('logs'=> $logs,'title'=>'Service Logs'));
}
Then my view :
<table class="table table-striped table-bordered">
<thead>
<tr>
<td>Domain</td>
<td>Port</td>
<td>Checktime</td>
<td>Status</td>
<td>Responce</td>
</tr>
</thead>
<div class="container">
#foreach($logs as $key => $value)
<tr>
<td>{{ $value->domain }}</td>
<td>{{ $value->service_port }}</td>
<td>{{ $value->checktime }}</td>
<td class="text-center">
#if( $value->status == 'up' ) <img src="../img/up3.png" />
#elseif( $value->status == 'down' ) <img src="../img/down3.png" />
#else <img width="30" height="30" src="../img/warning_icon.png" />
#endif
</td>
<td>{{ $value->response_time }}</td>
</tr>
#endforeach
</div>
</table>
{{$logs->links();}}
So please if someone has any idea i will be so appreciative
You do not need to use get when you are using pagination. Your query should simply be:
$logs = DB::table('services')->join('logs', function($join)
{
$join->on('services.id', '=', 'logs.service_id');
})->paginate(10);
In fact you can remove that closure as well since this really isn't a complex join.
$logs = DB::table('services')
->join('logs', 'services.id', '=', 'logs.service_id')
->paginate(10);
From the docs (http://laravel.com/docs/pagination) it seems that the correct usage is:
$logs = DB::table('services')
->join('logs', function($join)
{
$join->on('services.id', '=', 'logs.service_id');
})
->paginate(10); //removed get()

Categories