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:

enter image description here

and when call {{ $user->totalhours }}

i error:

enter image description here

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

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -