I am trying to convert my existing insert methods into query scopes so that I can reuse them and have a more DRY approach.
This is what I want to convert into a Larvel query scope:
$time = new Time;
$time->employee_id = $input['user_id'];
$time->day = Carbon::now()->toDateString();
$time->clock_in = Carbon::now()->toTimeString();
$time->save();
This is what I have now for the query scope:
public function scopeClockIn($query, $userID) {
$query->employee_id = $userID;
$query->day = Carbon::now()->toDateString();
$query->clock_in = Carbon::now()->toTimeString();
$query->save();
}
This is how I am calling the above query scope:
$time = Time::clockIn($input['user_id']);
But I get the error:
Call to undefined method Illuminate\Database\Query\Builder::save()
I have also tried:
$time = new Time::clockIn($input['user_id']);
but when I try with the new
keyword I get an error:
syntax error, unexpected 'clockIn' (T_STRING), expecting variable (T_VARIABLE) or '$'
Google has not been very helpful I have found other questions with the same error message but they are trying to retrieve results not insert them. Hoping someone here can help me figure out what I did wrong.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire