The issue with the current implementation is that the $companies variable in the show method of the CompanyController is not being used to retrieve the projects related to the specific company being shown. Instead, it is being used to retrieve all companies and their projects.
To fix this, we need to modify the show method to retrieve only the projects related to the specific company being shown. We can do this by using the $company variable that is passed to the method.
Here's the updated show method:
public function show(Company $company)
{
$projects = $company->projects;
return view('companies.show', compact('projects'));
}
In the updated method, we retrieve the projects related to the $company variable using the projects method defined in the Company model. We then pass the $projects variable to the companies.show view instead of the $companies variable.
Next, we need to update the blade file to use the $projects variable instead of the $companies variable to display the projects related to the company being shown.
Here's the updated blade file:
@include('layouts.navbar')
<div class="w-9/12 rounded-lg justify-center mx-auto border-2 mt-8">
<h1 class="font-sans text-2xl px-8 font-semibold">Description</h1>
@foreach ($projects as $project)
<div class="border-2 mt-3 p-3 rounded-lg justify-center mx-auto w-7/12 mb-4">
<h1 class="font-sans text-xl">{{ $project->name }}</h1>
<h1 class="font-sans text-xl">{{ $project->status }}</h1>
<h1 class="font-sans text-xl">{{ $project->description }}</h1>
<h1 class="font-sans text-xl font-semibold">Employee Details: name description date created</h1>
<ul>
@foreach ($project->stages as $stage)
<li>{{ $stage->name }}</li>
<li>{{ $stage->description }}</li>
<li>{{ $stage->created_at }}</li>
@endforeach
</ul>
</div>
@endforeach
</div>
In the updated blade file, we use the $projects variable to loop through the projects related to the company being shown. We then display the project details and the stages related to each project.
With these changes, the show method of the CompanyController should now display all the projects related to the company being shown.