Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

266
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda