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();
}
Set up your model relations and you can just do this
function index()
{
$appointments = Auth::user()->appointments;
return view ('appointment/userappointments',['appointments' => $appointments]);
}
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();
}