NightM4re
1 year ago

Scope Problem in User Model

Posted 1 year ago by NightM4re

Hi everyone, I have problem making a scope in Laravel.

User.php

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'nombre', 'apellidos', 'rut', 'direccion', 'comuna' ,'ciudad', 'region', 'telefono', 'tipo', 'puntos', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function getNombreCompletoAttribute()
    {
        return $this->nombre . ' ' . $this->apellidos;
    }

    public function scopeNombre($query, $nombre)
    {
        if ($nombre != "") {
            $query->where('nombre', $nombre);
        }
    }
}

VentaController.php

public function usuarios(Request $request){
        $users = User::nombre($request->nombre)->paginate(20);
        return view('admin.usuarios', compact('users'));
    }

And I received this error

BadMethodCallException in Builder.php line 2405: Call to undefined method Illuminate\Database\Query\Builder::nombre()

Please Help, I use the same method for scoped other model and works great but with the user model I cant make work.

Please sign in or create an account to participate in this conversation.