A
A
Archakov Dennis2016-06-15 16:53:25
Laravel
Archakov Dennis, 2016-06-15 16:53:25

How to add your own method to the User model?

I want to add my own method that will calculate the number of messages and set this amount in the user field = rating.
There is a model that was originally in Laravel:

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];


    public function updateRating($query)
    {
        ...
    }



}

But when I call like this:
$user = User::where('name','=',$username);
$user->increment('answ_num');
$user->updateRating();

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

What is the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Pochepko, 2016-06-15
@archakov06

$user = User::where('name','=',$username)->get();
You did not receive the model itself. You just got a request builder

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question