1

In my User Model i have this getter method:

public function getWorkedHoursInRangeAttribute($start, $end)
{
    $sum = [];
    foreach($this->workedTimes as $item) {

        if( ($item->start_time->gt($start)) && ($item->end_time->lt($end)) ){

            array_push($sum, ceil($item->end_time->diffInMinutes($item->start_time->addMinutes($item->break_time_min))/60 * 4) / 4 );

        }

    }

    return array_sum($sum);
}

And in my view i post this

function(start, end) {

 $.ajax({
        type: "POST",
        url: '/users/workedhours',
        headers: {'X-CSRF-TOKEN': '{{ csrf_token() }}' },
        data: {"start": start.format("DD.MM.YYYY HH:mm"), "end": end.format("DD.MM.YYYY HH:mm")},

and i have this in my Controller

public function getWorkedHoursForRange(Request $request)
{

    $start = Carbon::parse($request->start);
    $end = Carbon::parse($request->end);


    return response()->json(['msg' => $start], 200);
}

and this route:

Route::post('users/workedhours', 'UsersController@getWorkedHoursForRange');

How can i take this start and end variables from ajax to my Model method and make calculation?

I will probably need to do something in my Controller...but what?

UPDATE:

in view i have this foreach loop for my table:

     <tbody>
     @foreach ($users as $user)
         <tr>
             <td><a href="{{ url('/user', $user->id)  }}">{{ $user->name }}</a></td>
             <td>{{ $user->total_time_current_year }}</td>
             <td>{{ $user->total_time_last_year }}</td>
             <td>{{ $user->total_time_current_month }}</td>
             <td>{{ $user->total_time_last_month }}</td>
             <td>here i need the calculation for date range</td>
         </tr>
     @endforeach
     </tbody>
2
  • You should convert make a new string called total and just get the time difference between start and end variables and that will be your difference and just return that in the response. Commented Nov 22, 2016 at 10:16
  • I do not know what authentication mechanism you have set up, but normally $user = Auth::user() will get you the current authenticated user and then you could call the getWorkedHoursInRangeAttribute() function on that user, passing in $start and $end. Hope it helps Commented Nov 22, 2016 at 10:17

1 Answer 1

1

You need two attributes in your model: start and end. That way you are able to access them in your accessor.

protected $start;
protected $end;

public function getWorkedHoursInRangeAttribute()
{
    $sum = [];
    foreach($this->workedTimes as $item) {

        if( ($item->start_time->gt($this->start)) && ($item->end_time->lt($this->end)) ){

            array_push($sum, ceil($item->end_time->diffInMinutes($item->start_time->addMinutes($item->break_time_min))/60 * 4) / 4 );

        }

    }

    return array_sum($sum);
}

You than make your AJAX call to the controller, loop through the users, set start and end vars to each user-model and return all users to the view. There you can access your accessor-attribute.

public function getWorkedHoursForRange(Request $request)
{

    $start = Carbon::parse($request->start);
    $end = Carbon::parse($request->end);

    $users = App\User::all();
    foreach($users as $user) {
        $user->start = $start;
        $user->end = $end;
    }


    return response()->json(['users' => $users], 200);
}
Sign up to request clarification or add additional context in comments.

7 Comments

OK now i see that i have forgot to put my view and because of that you didn't get the whole picture....so i will update my question with view and you will understand it better. sorry guys :(
@lewis4u I think you need to specify what workedTimes is in your model. Is this set?
$calc = App\User::calculate($start, $end); its wrong used here. its not a static attr
@sleepless yes...i already have a few get methods with worked times but this one should be dynamic
@sleepless if you have time for team viewer you can see it!?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.