Get sum of minutes of specific project - laravel

I want to get total minutes. Example: joylinkhk 240 + 180 = 420. How can I get a total minute of a specific user?
controller
public function search(Request $request) {
$date = explode(' - ', $request->date);
$auth = Auth::user();
$hourLog = Hourlog::with('project', 'user');
if ($auth->user_type == 1) {
$hourLog->where("user_id", $auth->id);
}
$data = [
"date" => $date,
// 'projects' => Project::whereIn('id', $request->project)->get(),
'projects' => Project::with('users')->whereIn('id', $request->project)->get(),
'users' => User::with(['hourlog' => function ($query) use ($request) {
$query->whereIn('project_id', $request->project);
}])->whereIn('id', $request->user)->get(),
];
return view('cms.projectreport.projectreport-list', $data);
}
HTML view
#foreach($users as $user)
<tr>
<td>{{$user->name}}
</td>
#foreach($user->hourlog as $hourlogs)
<td>{{$hourlogs->hour_work}}
</td>
#endforeach
</tr>
#endforeach

Laravel collections are very powerful and offer such functionality out of the box.
Below is a example of how you can take a collection of arrays and sum by its property hour_work. The collect function is a quick way to create a collection like the $user->hourlog already is.
collect([
['hour_work' => 198],
['hour_work' => 93],
['hour_work' => 51],
['hour_work' => 112],
])->sum('hour_work');
// Result: 454

As #danny-van-der-sluijs said, you can use sum on a collection
#foreach($users as $user)
<tr>
<td>{{$user->name}}</td>
#foreach($user->hourlog as $hourlogs)
<td>{{$hourlogs->hour_work}}</td>
#endforeach
</tr>
#endforeach
<tr>
<td>Grand Total</td>
<td>{{ $users->sum(fn($user) => $user->hourlog->sum('hour_work')) }}</td>
</tr>
Note:
$users->sum(fn($user) => $user->hourlog->sum('hour_work'))
Is an arrow function that came with php 7.4, If you are using a version below 7.4 you have to do it like this
$users->sum(function($user) {
return $user->hourlog->sum('hour_work');
});

You can sum in foreach loop, but get the result outside of foreach loop. You need to define first the variable outside of foreach $sum_total = 0;, then you can sum inside foreach like this :
<?php $sum_total = 0; ?> // define here
#foreach($users as $user)
<tr>
<td>{{$user->name}}
</td>
#foreach($user->hourlog as $hourlogs)
<td>{{$hourlogs->hour_work}}
</td>
<?php
$sum_total += (intval)$hourlogs->hour_work
?>
#endforeach
</tr>
<tr>
<td>{{ $sum_total }}</td>
</tr>
#endforeach

Related

Laravel - Undefined property: Illuminate\Database\MySqlConnection::$username

I wrote a query to pass parameter from view to controller, and I got this error:
Undefined property: Illuminate\Database\MySqlConnection::$username
View Passing the parameter
<td width="25%"><a class="btn btn-info" href="{{ route('gamesListDetail',$game->id) }}">{{ $game->name }}</a></td>
Receiving Controller
public function gamesListDetail($id = null)
{
$gamelists = DB::table("platform_games")
->select("platform_games.id", "platform_games.username","game_player.game_id")
->join("game_player","game_player.game_id","=","platform_games.id")
->where('platform_games.id',$id)
->take(5);
return view('soccerrave.games.gamesListDetail', compact('gamelists'));
}
Receiving View
<tbody>
#foreach($gamelists as $key => $gamelist)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $gamelist->username }}</td>
</tr>
#endforeach
<tr>
<td colspan="8">
{{ $gamelists->links() }}
</td>
</tr>
</tbody>
I expect the view to display top 5 data based on the parameter. But I got this error:
Undefined property: Illuminate\Database\MySqlConnection::$username
Documentation says:
skip / take
To limit the number of results returned from the query, or to skip a
given number of results in the query, you may use the skip and take
methods:
$users = DB::table('users')->skip(10)->take(5)->get();
So by example we see that after take method there must be get() call.
So fix Your code:
public function gamesListDetail($id = null)
{
$gamelists = DB::table("platform_games")
->select(
"platform_games.id",
"platform_games.username",
"game_player.game_id"
)
->join(
"game_player",
"game_player.game_id", "=", "platform_games.id"
)
->where('platform_games.id', $id)
->take(5)
->get(); // this one is required
return view('soccerrave.games.gamesListDetail', compact('gamelists'));
}

Display multiple variable data separately from multiple tables on single view in blade template in laravel

I want to display two variable data on same view in a foreach loop
public function getWalletList()
{
$data = Data::orderBy('id','desc')->get()->toArray();
$user_ids = array_column($data, 'id');
$user_data = User::whereIn('id',$user_ids)->get()->toArray();
$d= array('data'=>$data,'user'=>$user_data);
return view('wallet_list', compact('d'));
}
So how to diplay data on laravel blade template
#foreach($d as $value)
<tr>
<td>{{$value['wallet']['name']}}</td>
<td>{{$value->user_id}}</td>
<td>{{$value->price}}</td>
<td>{{$value->price}}</td>
<td>{{$value->updated_at}}</td>
</tr>
#endforeach
why dont you use two foreach ?
#foreach($d as $value)
<tr>
<td>{{$value['wallet']['name']}}</td>
<td>{{$value->user_id}}</td>
<td>{{$value->price}}</td>
<td>{{$value->price}}</td>
<td>{{$value->updated_at}}</td>
</tr>
#endforeach
#foreach($b as $value)
<tr>
<td>{{$value['wallet']['name']}}</td>
<td>{{$value->user_id}}</td>
<td>{{$value->price}}</td>
<td>{{$value->price}}</td>
<td>{{$value->updated_at}}</td>
<tr>
#endforeach

how to get data from laravel5.5?

I am using Laravel 5.5 and I want to view my data in database from my view page (home.blade.php)
<table>
<tbody>
#foreach($home as $key => $data)
<tr>
<th>{{$data->created_at}}</th>
<th>{{$data->category}}</th>
<th>{{$data->reportitle}}</th>
<th>{{$data->reportdetails}}</th>
<th>{{$data->seenstat}}</th>
<th>{{$data->actionstat}}</th>
<th>{{$data->donestat}}</th>
</tr>
#endforeach
</tbody>
</table>
In my home.blade.php I have an empty table and I want to show data from database fyp:
Route::get('home', function () {
$fyp = DB::table('reports')->get();
return view('home', ['fyp' => $fyp]);
});
This must work (as #Nazmul said):
#foreach($fyp as $data)
<tr>
<th>{{$data->created_at}}</th>
<th>{{$data->category}}</th>
<th>{{$data->reportitle}}</th>
<th>{{$data->reportdetails}}</th>
<th>{{$data->seenstat}}</th>
<th>{{$data->actionstat}}</th>
<th>{{$data->donestat}}</th>
</tr>
#endforeach
just follow the code
Route::get('home', function () {
$home = DB::table('reports')->get();
return view('home', compact('home'));
});
After in your view page
<table>
<tbody>
#foreach($home as $key => $data)
<tr>
<th>{{$data->created_at}}</th>
<th>{{$data->category}}</th>
<th>{{$data->reportitle}}</th>
<th>{{$data->reportdetails}}</th>
<th>{{$data->seenstat}}</th>
<th>{{$data->actionstat}}</th>
<th>{{$data->donestat}}</th>
</tr>
#endforeach
</tbody>
</table>
I hope this will help you.

Laravel Excel - blank sheets when creating multiple sheets

I had this working, using Laravel 4 and Laravel Excel to export data from mySQL. I have objects for Regtype and Attendee and I want one sheet per regtype with all related attendees. I had a single sheet of attendees exporting great. Now I've added a loop for the regtypes and I'm able to get multiple sheets but all sheets are blank!
Perhaps I have to use shareView? I wouldn't think so.. I get no errors and none of the data I pass to the blade template is displaying.
My export function:
public function export()
{
$date = date('Y_m_d');
\Excel::create('CMO_Connect_Attendees_Export_'.$date, function($excel) {
$regtypes = Regtype::all();
foreach ($regtypes as $regtype) {
if( $regtype->attendees(3)->count() ) {
$excel->sheet($regtype->name, array('regtype' => $regtype), function($sheet) {
$date = date('Y_m_d');
$attendees = new Attendee;
$atts = Attendee::where('block_id', '=', \Input::get('block_id'))
->where('regtype_id', '=', $regtype->id)
->get();
$sheet->setStyle(array(
'font' => array(
'name' => 'Arial',
'size' => 12,
'bold' => false
)
));
$sheet->loadView('attendees.export', array('atts' => $atts))->with('curdate',$date)->with('regtype_name',$regtype->name);
$atts = '';
});
} //endif
} //endforeach
$excel->export('xls');
});
}
And the blade template I'm passing data to (I pass the attendees and also the date and name of the regtype to display at the top of the sheet:
<html>
<table>
<tr>
<td colspan="11">Attendees Export - {{ $curdate }} - {{ $regtype_name }}</td>
</tr>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Company</td>
<td>Title</td>
<td>Email</td>
<td>Phone</td>
<td>Address</td>
<td>Addres 2</td>
<td>City</td>
<td>State</td>
<td>Zip</td>
<td>Date Created</td>
</tr>
#foreach($atts as $att)
<tr>
<td> {{ $att->firstname }} </td>
<td> {{ $att->lastname }} </td>
<td> {{ $att->company }} </td>
<td> {{ $att->title }} </td>
<td> {{ $att->email }} </td>
<td> {{ $att->phone }} </td>
<td> {{ $att->address }} </td>
<td> {{ $att->address2 }} </td>
<td> {{ $att->city }} </td>
<td> {{ $att->state }} </td>
<td> {{ $att->zip }} </td>
<td> {{ $att->created_at }} </td>
</tr>
#endforeach
</table>
</html>
Thanks for any help!
I sorted out - below is my export function now. I updated the vendor files through composer (realized I was on a slightly older version) and revised my code with use($regtype when calling the $sheet method (passing the regtype object into the sheet). I assumed that you'd pass variables or objects into the sheet the same way you pass data into the blade template but that's not correct.
public function export()
{
$date = date('Y_m_d');
\Excel::create('CMO_Connect_Attendees_Export_'.$date, function($excel) {
$excel->setTitle('CMO Connect Attendee Data Export');
$excel->setCreator('Dylan Glockler')
->setCompany('Bryan Allen Events');
$excel->setDescription('All attendee event data for Adobe CMO Connect');
$regtypes = Regtype::all();
$summary = '';
foreach ($regtypes as $regtype) {
if( $regtype->attendees(3)->count() ) {
$summary = $summary . $regtype->name;
$summary = $summary . ': '.$regtype->attendees(3)->count();
$summary = $summary . ' | ' . $regtype->id . '<br />';
$excel->sheet($regtype->name, function($sheet) use($regtype) {
$date = date('Y_m_d');
$atts = Attendee::where('block_id', '=', \Input::get('block_id'))
->where('regtype_id', '=', $regtype->id)
->get();
$sheet->setStyle(array(
'font' => array(
'name' => 'Arial',
'size' => 12,
'bold' => false
)
));
$sheet->loadView('attendees.export')->with('curdate',$date)->with('atts',$atts)->with('regtype_name',$regtype->name)->with('att_count',$regtype->attendees(3)->count());
});
} //endif
} //endforeach
$excel->export('xls');
});
}

increment row number with laravel pagination

How to make increment row number with laravel pagination ? When i use pagination and i go to page 2 and above it will back to beginning. for example i will paginate(3)
<thead>
<tr>
<th>No</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
#foreach ($telephone->results as $telp)
<tr>
<td>
{{$i++}}
</td>
<td>{{ $telp->name }}</td>
</tr>
#endforeach
</tbody>
when i go to page 2 the number will start from 1 again.
i need to make it when i go to page 2 it will start from 4
In Laravel 5.3 you can use firstItem():
#foreach ($items as $key => $value)
{{ $items->firstItem() + $key }}
#endforeach
The below works with laravel 5.4
<?php $i = ($telephone->currentpage()-1)* $telephone-
>perpage() + 1;?>
#foreach($telephone as $whatever)
<td> {{ $i++ }}</td>
#endforeach
Edited
The below works for laravel 5.7 above
#foreach ($telephone as $key=> $whatever)
<td>{{ $key+ $telephone->firstItem() }}</td>
#endforeach
You should be able to use the getFrom method to get the starting number of the current pages results. So instead of setting $i = 1; you should be able to do this.
<?php $i = $telephone->getFrom(); ?>
In Laravel 3 there is no getFrom method so you need to calculate it manually.
<?php $i = ($telephone->page - 1) * $telephone->per_page + 1; ?>
Laravel 5.3
#foreach ($products as $key=>$val)
{{ ($products->currentpage()-1) * $products->perpage() + $key + 1 }}
#endforeach
For Laravel 6.2:
$loop - just in case, is a built-in instance in Blade
#foreach($array as $item)
<tr class="table-row">
<td class="site-id">
{{($array->currentPage() - 1) * $array->perPage() + $loop->iteration}}
</td>
</tr>
#endforeach
</table>
You can simply add the following line
$i = ($telephone->currentpage()-1)* $telephone->perpage();
in place of
$i = 1;
#php($sl = ($yourVariable->perPage() * $yourVariable->currentPage()) - ($yourVariable->perPage() - 1))
#foreach($yourVariable as $item)
<td>{{ $item->key_name }}</td>
.....
#php($sl++)
#endforeach
In Laravel 6
#php $i = ($data->currentpage()-1)* $data->perpage() + 1;#endphp
#foreach($data as $banner)
<tr>
<td>{{$i}}</td>
<td><img src="{{ URL::to('/') }}/dist/img/{{$banner->name}}" height="250" width="600"></td>
</tr>
#php $i += 1; #endphp
#endforeach
If you are using Laravel Pagination. It works very well
The backend
public function index()
{
return view('your.telephone.index', [
'telephone' => Telephone::paginate(15)
]);
}
Blade front end
<td>{{ (($telephone->currentPage() * 10) - 10) + $loop->iteration }}</td>
And don't forget embed the page
{{ $telephone->links() }}
For $loop->iteration
See the doc here: https://laravel.com/docs/7.x/blade#the-loop-variable
For $telephone->currentPage()
See the doc here: https://laravel.com/docs/7.x/pagination#paginator-instance-methods
Or avoid php tags completely by
#foreach ($users as $key => $user)
{{ (Input::get('page', 1) - 1) * $users->getPerPage() + $key + 1 }}
#endforeach
You can use it in your controller. example given below
$records = Table::paginate(20);
return view('yourview',compact('records')->with('i', ($request->input('page', 1) - 1) * 20);
Note: paginate value must be equal to with() function vlaue
after that you can use $i variable in you blade file. controller calculate value according to page number and return to blade
In blade file. use inside the loop
{{ ++$i }}
Hopefully this will help you
Laravel 8++
$loop->iteration is available out of the box inside loop.
{{ ($barangs->currentPage() - 1) * $barangs->count() + $loop->iteration }}
I'm using this, in Laravel 8, accessing $loop, and links()->paginator:
#foreach ($users as $user)
<tr>
<td class="text-center">{{ ($users->currentPage() - 1) * $users->links()->paginator->perPage() + $loop->iteration }}</td>
<td class="text-center">{{$user->name}}</td>
</tr>
#endforeach
Which works properly even when the final page is only partially filled.
Could also use $users->perPage() in place of $users->links()->paginator->perPage()

Resources