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
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.
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();
Please or to participate in this conversation.