I am new to Laravel and coding. Trying to show Month name (Array) in table but not able to do the same. I tried few things like "json_encode" but got error each time.
My controller code is
$months = array();
for ($i = 0; $i < 12; $i++) {
$timestamp = mktime(0, 0, 0, date('n') - $i, 1);
$months[date('n', $timestamp)] = date('M-Y', $timestamp);
}
Output is
array:12 [▼
3 => "Mar-2022"
2 => "Feb-2022"
1 => "Jan-2022"
12 => "Dec-2021"
11 => "Nov-2021"
10 => "Oct-2021"
9 => "Sep-2021"
8 => "Aug-2021"
7 => "Jul-2021"
6 => "Jun-2021"
5 => "May-2021"
4 => "Apr-2021"
]
View file
<table id="incometable" class="table m-0 table table-bordered table-hover table" data-order='[[ 0, "desc" ]]'>
<thead>
<tr>
<th align="center">Month</th>
<th>Milk Sale Amount (Rs.)</th>
<th>Animal Sale Amount (Rs.)</th>
<th>Other Sale Amount (Rs.)</th>
</tr>
</thead>
<tbody>
#foreach ($months as $item )
<tr>
<td>{{ $item->$months }}</td>
</tr>
#endforeach
</tbody>
</table>
Thanks in Advance
During your #foreach() loop, $item is not an Object, it is a String 'Mar-2022' through 'Apr-2021'. You need to adjust your code:
#foreach($months as $key => $label)
<tr>
<td>{{ $label }}</td>
<tr>
#endforeach
If you need the 1 through 12 values, they are available in each iteration as $key.
Related
I want to sum total yes in a row like following
id
user_id
eligible
asset
requirement
1
1
yes
no
yes
2
1
yes
yes
yes
3
2
no
yes
no
Result should be following:
user_id
yes_count
1
5
2
1
I consider that table as Items.
User model
public function items()
{
return $this->hasMany(Item::class);
}
Controller
$users = User::withCount([
'items as items_eligible_yes_count' => fn($q) => $q->where('eligible', 'yes'),
'items as items_asset_yes_count' => fn($q) => $q->where('asset', 'yes'),
'items as items_requirement_yes_count' => fn($q) => $q->where('requirement', 'yes'),
])
->get();
View
<table>
<thead>
<tr>
<th>user_id</th>
<th>yes_count</th>
</tr>
</thead>
<tbody>
#foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->items_eligible_yes_count + $user->items_asset_yes_count + $user->items_requirement_yes_count }}</td>
</tr>
#endforeach
</tbody>
</table>
I get an error like this
ErrorException
This my link index
<td>{{$cmface}}</td>
This my Routes
Route::get('/detail_face_to_face/{id}', 'FaceToFaceController#detail')->name('detail_face_to_face');
This my Controller
public function detail($id)
{
$xclass = DB::table('face_to_face')->select('Class')->where('id', $id)->first();
$tface = DB::table('tbclass')
->select('tbclass.F_Name','tbclass.Class')
->where('tbclass.Class', $xclass)
->whereNotExists(function ($query) use ($id) {
$query->select('id_user')
->from('absent')
->where([['absent.id_face_to_face', $id],['absent.type_face_to_face', '1'],])
->whereRaw('absent.id_user = tbclass.ID_No');
})
->orderBy('tbclass.F_Name', 'ASC')
->paginate(10);
return view('face_to_face.detail',['tface' => $tface]);
}
This my face_to_face.detail page
<table class="table">
<thead style="white-space:nowrap;">
<tr>
<th>No</th>
<th>Name</th>
<th>Class</th>
</tr>
</thead>
<tbody style="white-space:nowrap;">
#if($tface->count()===0)
<tr>
<td class="table-success text-center" colspan="10"><< Data is Empty >></td>
</tr>
#else
#foreach($tface as $no => $tm)
<tr>
<td>{{ ++$no + ($tface->currentPage()-1) * $tface->perPage() }}</td>
<td>{{$tm->F_Name}}</td>
<td>{{$tm->Class}}</td>
</tr>
#endforeach
#endif
</tbody>
</table>
{{ $tface->links() }}
if I click the link on the index page, i get that error
Can anyone help ???
Could you dd($xclass)?
Error is in, ->where('tbclass.Class', $xclass). You probably do ->where('tbclass.Class', $xclass->Class)
Regard
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
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');
});
}
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()