php - Laravel Model function using relationship -
i made function in view summing total hours
<?php $sum = []; foreach($user->workedtimes $item){ array_push($sum, ($item->end_time->diffinhours($item->start_time))); } echo array_sum($sum); ?>
and in blade view fine, want same code in model can call user->totalhours in blade
and made function in model:
and when call {{ $user->totalhours }}
i error:
how make work model??
you should use method brackets this:
{{ $user->totalhours() }}
but if use laravel accessor this:
<?php namespace app; use illuminate\database\eloquent\model; class user extends model { public function gettotaltimeattribute() { // logic here ... $sum = []; foreach($this->workedtimes $item) { array_push($sum, $item->end_time->diffinhours($item->start_time)); } return array_sum($sum); } }
and access in blade this:
{{ $user->total_time }}
hope helps!
Comments
Post a Comment