Jurchello
Jurchello

Reputation: 33

laravel mutator doesn't work

Why my Laravel mutator does't work? I want to increment values in "like" column. Commented row works good, but mutator doesn't instead of commented one.

public function updateLike(Request $request){
    $comment = Comment::find($request->id);
    //$comment -> like = $comment-> like + 1;
    $comment -> save();
}

public function setLikeAttribute($value){
    return $this -> like = $value + 1;
}

Upvotes: 0

Views: 351

Answers (1)

Hanlin Wang
Hanlin Wang

Reputation: 779

you need use this in mutator

$this->attributes['like'] = $value + 1;

but for your case, mutator is not used for automatically increase a value, you should use your commented code, not mutator.

Upvotes: 1

Related Questions