Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

268
Vistas
Is there a explanation for my model function can't be used in my controller?

I'm building a laravel project which is a community platform, so it's gonna need a follower logic (pretty similar to twitter, instagram, etc). I already created the logic for authentication and profile, but, when researching and writing the code for the followers state and check if the user is following someone, i got the functions on my model, which now is something like:


namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'phone',
        'description',
        'profilepicture',
        'status',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function following()
    {
        return $this->belongsToMany('App\Models\User', 'followers', 'follower_user_id', 'user_id')->withTimestamps();
    }

    public function isFollowing(User $user)
    {
        return !is_null($this->following()->where('user_id', $user->id)->first());
    }
}

And on my Profile Controller, I have:


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;


class Profile extends Controller

{
    public function show($id)
    {
    $user = User::where('id', $id)->firstOrFail();
    $me = Auth::user();
    $is_edit_profile = (Auth::id() == $user->id);
    $is_follow_button = (!$is_edit_profile) && (!$me->isFollowing($user));
    return view('profile', ['user' => $user, 'is_edit_profile' => $is_edit_profile, 'is_follow_button' => $is_follow_button]);
    }
}

But VSCode says that i have a undefined method isFollowing in my controller, in the line:

$is_follow_button = (!$is_edit_profile) && (!$me->isFollowing($user));

Someone have a clue of why is this happening? I'm using Laravel 8. It's one of my first big projects, so previously sorry for any rookie mistake. Thanks for your time and help!

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Auth::user() returns an object of type Illuminate\Contracts\Auth\Authenticatable which does not implement isFollowing

Option 1 : You can add @var annotation to specify the type of your object

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;


class Profile extends Controller

{
    public function show($id)
    {
    $user = User::where('id', $id)->firstOrFail();
    /** @var User $me */
    $me = Auth::user();
    $is_edit_profile = (Auth::id() == $user->id);
    $is_follow_button = (!$is_edit_profile) && (!$me->isFollowing($user));
    return view('profile', ['user' => $user, 'is_edit_profile' => $is_edit_profile, 'is_follow_button' => $is_follow_button]);
    }
}

Option 2 : You can extends the Auth facade by creating a new facade with the expected return type :

namespace App\Extensions\Facades;

use App\Models\User;

/**
 * @method static User user()
 */
class Auth extends \Illuminate\Support\Facades\Auth
{

}

And then you can use this facade instead of the previous one

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Extensions\Facades\Auth;
use App\Models\User;


class Profile extends Controller

{
    public function show($id)
    {
    $user = User::where('id', $id)->firstOrFail();
    $me = Auth::user();
    $is_edit_profile = (Auth::id() == $user->id);
    $is_follow_button = (!$is_edit_profile) && (!$me->isFollowing($user));
    return view('profile', ['user' => $user, 'is_edit_profile' => $is_edit_profile, 'is_follow_button' => $is_follow_button]);
    }
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda