MartinZeltin
4 weeks ago

How to create a static and non-static method on Model?

Posted 4 weeks ago by MartinZeltin

I would like to create a new method on my model where I could call it in 2 ways

class UsersController extends Controller
{
    public function index()
    {
        // Calling statically
        return User::searchByName(request('name'));

        // Calling non-statically
        return User::active()->searchByName(request('name'));
    }
}

But when I create it like this it only works statically

class User extends Model
{
    public static function searchByName($name)
    {
        return self::where('name', 'like', "$name%")
                   ->take(3)
                   ->get()
                   ->pluck('name');
    }
}

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