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

187
Visualizações
Laravel - Comparing a database value with a variable

I have a basic appointment system set up, however everybody logged in can see all appointments, so what I'm trying to do is make it so users can only see their appointments. To do this I'm trying to get the logged in users Id then display all the appointments that have this user_id value (Appointments is a junction table)

I know my code is probably awful but any help is appreciated, thanks

function index()
    {
        $logged_user_id = Auth::user()->id;
        $user = User::find($logged_user_id);

        $appointments = Appointment::where('user_id', '=' $user)->get();

        return view ('appointment/userappointments',['appointments' => $appointments]);
//      $appointments = Appointment::all();
    } 
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Set up your model relations and you can just do this

function index()
{
    $appointments = Auth::user()->appointments;
    return view ('appointment/userappointments',['appointments' => $appointments]);
} 
over 4 years ago · Santiago Trujillo Relatório

0

You already have the user id, dont have to fetch User info.

function index()
{
    $logged_user_id = Auth::user()->id;
    $appointments = Appointment::where('user_id', '=' $logged_user_id)->get();

    return view ('appointment/userappointments',['appointments' => $appointments]);
    //$appointments = Appointment::all();
} 
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