Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!