1

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();
    } 
1

2 Answers 2

3

Set up your model relations and you can just do this

function index()
{
    $appointments = Auth::user()->appointments;
    return view ('appointment/userappointments',['appointments' => $appointments]);
} 
Sign up to request clarification or add additional context in comments.

Comments

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();
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.