1

I'm trying to create a basic appointment system. When a user logs in, an if statement should display their personal appointments, but at the moment it's displaying everybody's appointments.

The aim of the if statement is to check their user ID to see if there are any appointments that use this user ID and display these appointments

My appointments table has a user_id, and my users table just has a regular id.

@section('content')
    <h1>Your Appointments</h1>
    @foreach ($appointments as $appointment)
        @if ($appointment->user->id == $appointment->user_id)
            <p>
                <a href="{{url('details/'.$appointment->id)}}" >{{$appointment->doctor->name}} : {{$appointment->time}} : {{$appointment->date}}</a>        
            </p>
        @endif
    @endforeach
@endsection

2 Answers 2

2

Instead of using this nested if statement we can reduce the logic in the view by selecting the appointments related to the authenticated user so in your controller instead of passing all the appointments we will do :

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

so in your view you can use only foreach($appointments as $appointment) to loop through them without needing to check if the appointment is related to the current user or not.

Sign up to request clarification or add additional context in comments.

Comments

1

It will always display everyone appointments because you are comparing appointment associated user->id with the same appointment->user_id.

I believe you should change your if statement and compare it with logged in user session id like below:

$logged_user_id = Auth::user()->id;

@if ($logged_user_id == $appointment->user_id)

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.