Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Birdy's avatar
Level 5

Relationship or not? Cant seem to solve the bleeding problem!

Hi guys after many stressfull hours i have had to turn to you for help!

within my models i have an appointment and a project model both have their own tables ect and what im trying to do is loop over the users appointments witch is working fine and would go something like the following example:

@foreach($appointments as $appointment) {{ $appointment->date}} {{ $appointment->time}} {{ $appointment->project_id }} @endforeach

Where i am struggling is the relationship to the projects table because i need to display the project name and description on the appointment page so for example when the foreach loops through the $appointments it will display the appointments and the correct project id's so for example:

1 2 3

However i then need to reference the projects table where the project_id is = to the $appointment->project_id

I have tried this several ways, a belongsTo method, hasOne and even tried the following: $project = Project::where('project_id', '=', $appointment->project_id)->get();

That works fine however it only ever gives me the first object from the foreach loop, Where as i need to be able to reference all three projects from provided by the appointments table if you see what im saying.

A perfect working example would be:

@foreach($appointments as $appointment) {{ $appointment->project()->projectName }} @endforeach

I have even tried this:

@foreach($appointments as $appointment) @foreach(\App\Project::where('project_id', '=', $appointment->project_id) as $project) {{$appointment->date}} {{$appointment->time}} {{$project->projectName}} @endforeach @endforeach

Everything works but only seems to give the record for the first project id lets say 1 even through the appointments foreach loop is providing three different appointments for three different project ids.

This to me seems so simple however after several hours an a full night coding i have basically lost myself in fustration and more than likely overlooking the most simple way as ive tried so many different ways and failed you tend to forget the basics.

Would really apprciate some support on this one guys, it will be much apprecaited and will help me get to the next stage!

Thanks all.

0 likes
6 replies
katifrantz's avatar

Can you help us format the code properly ( Use Markdown with GitHub-flavored code blocks. )? We can't see a thing . And please show us your models and how you have defined your relationships

1 like
willvincent's avatar
Level 54

So.. if I understand correctly, users have many appointments, appointments belong to a single project, and .. a single user?, and projects have many appointments?

Assuming that's all correct, you'd want these relationships defined:

User model:

public function appointments() {
  return $this->hasMany(Appointment::class);
}

Appointment model:

public function user() {
  return $this->belongsTo(User::class);
}

public function project() {
  return $this->belongsTo(Project::class);
}

Project model

public function appointments() {
  return $this->hasMany(Appointment::class);
}

Then your query to fetch all appointments, with project data, for the current user would be something like this:

$appointments = Appointment::where('user_id', '=', Auth::id())->with('project')->get();
1 like
Birdy's avatar
Level 5

@maitrefrantz @willvincent - My apologies for the very low explaination to my issue, I have the kids this weekend so its somewhat hectic trying to code and babysit haha.

I have not used migrations as i already had a project within php and phpmyadmin before switching to laravel and i have not had chance to convert all my tables to migrations properly as of yet, That being said i have had no issues with other relationships and ill give a quick overview to the database structure.

projects table has the following:

id (Primary with Auto-Inc) project_id (Unsigned) rest are just normal int/varchar columns.

My appointment model had to revert to the following:

public function project($projectId)
{
    return Project::where('project_id', '=', $projectId);
}

However i did have this:

public function project()
{
    return $this->hasMany(Project::class, 'project_id', 'project_id');
}

my appointments table has the following structure:

id (Primary also auto-inc) project_id (Unsigned) user_id(unsigned) rest is usual int/varchar columns

How records are stored is that the project_id on the likes of the appointment table is the same as the project_id column within the projects table although not actually referencing the (id) column its self as there is a dedicated project_id column that uses a (kind of) unique int that should be unique to the user at the time as it is created from their ip address followed by a timestamp and is placed into a 11 digit integer.

All tables such as appointments, payments, jobs, invoices ect ect all have a project_id column that refers to the exact project_id column within projects and has the value that is assigned to the project_id

so for example in the projects table here is a dummy records:

id = 1 project_id = 29371861087 title = description = ect ect..

and appointment example:

id = 1 project_id = 29371861087 appointment_date = (timestamp from carbon) appointment_time = 10:30am ect ect

and a payments table example:

id = 1 project_id = 29371861087 payment_type = paypal payment_amount = 20.00 ect ect

So i need to be able to loop through all the users appointments and where the appointments are showing for example here is some html:

{{ $appointment->appointment_date }}

{{ $appointment->appointment_time }}

{{ $appointment->project()->title }}

{{ $appointment->project()->description }}

Does that kind of give a better example?

I basically need to be able to fetch each project assocatiated with each appointment so when i loop over the appointments array i can do something similar to the above.

I really really do appreciate your help guys, Its people like yourselves and laracasts that help people like me gain a whole new adventure in life, Sorry if my spelling, grammer and explaination is not the best as im trying to type it as quickly as possible while i have 5 minutes free as the kids are glued to peppa pig on tv at the moment haha!.

Thanks.

Snapey's avatar

It should work as you have said here;

public function project()
{
    return $this->hasMany(Project::class, 'project_id', 'project_id');
}

If you have the above in your appointments model, then when you load appointments in your controller, you would want to add ->with('project') as @willvincent said

This will perform eager loading of the related model.

In your view, you then access the nested models (note there are no parentheses)

    {{ $appointment->project->description }}

This is because you are accessing description, an attribute of project, which in turn is an attribute of appointment.

1 like
Birdy's avatar
Level 5

@willvincent @Snapey - Sorry for the late reply, I greatly apprciate your help once again :) thanks for taking time out of your day to assist and help me achieve the result i needed, Take care and thanks.

Snapey's avatar

no problem - onwards and upwards !

1 like

Please or to participate in this conversation.