Laravel save () not defined

I am trying to transform existing insertion methods in the query area so that I can reuse them and use a more DRY approach.

This is what I want to convert to a Larvel request 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 for the request area:

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 call the specified request scope:

$time = Time::clockIn($input['user_id']);

But I get the error:

Call to undefined method Illuminate\Database\Query\Builder::save()

I also tried:

$time = new Time::clockIn($input['user_id']);

but when I try to use the keyword new, I get an error:

syntax error, unexpected 'clockIn' (T_STRING), expecting variable (T_VARIABLE) or '$'

Google did not help much. I found other questions with the same error message, but they try to get the results without pasting them. Hope someone here helps me figure out what I did wrong.

+4
source share
1

.

Laravel Builder , . SQL-. .

, . Time::all() Time::where('something', '=', 'something')->get(), , Builder.

, Builder. - , .


, . . , , , . - :

public static function createFromUserId($userID) {
    $time = new static();
    $time->employee_id = $userID;
    $time->day         = Carbon::now()->toDateString();
    $time->clock_in    = Carbon::now()->toTimeString();
    $time->save();
    return $time;
}

static, , , . : :: in Time::clockIn() , , static.

, , - . :

$time = Time::createFromUserId($userID);

, .

+5

All Articles