I have 2 tables in my db, 1 for Collabs and 1 for Projects
I want when I view a project, to display the collabs based on that project (if i press view on a project for example project with id = 10, to display all Collabs for the project that is id 10).
For Collabs table I have id_project
that is wanted to be in relationship with id
from Project table, so when I insert a new collab in my Collabs table it takes the id from the project I inserted.
For now, this is how I display the collabs, and i display them all for all projects but I don’t want that.
@forelse ($istoric as $istProj)
<div class="mb-3">
<table class='table'>
<tr class="table-row-heads">
<th>Id Proiect</th>
<th>Tip actiune </th>
<th>Colaborator </th>
<th>Suma </th>
<th>Data </th>
</tr>
<tr class="table-row-data">
<td>{{ $istProj->id_proiect }}</td>
<td>{{ $istProj->action_type }}</td>
<td>{{ $istProj->colaborator_id }}</td>
<td>{{ $istProj->suma }}</td>
<td>{{ $istProj->data }}</td>
</tr>
</table>
</div>
@empty
<div class="card-body">
<h2>Nu au fost gasite inregistrari</h2>
</div>
@endforelse
2
Answers
If you use model Collab, and within you have project relation , than you can use
Or you can use query builder as well
Just adjust it according what you really need.
You should really consider reading and watching more videos on how relationships and eloquent works, I hope this below is a good reference for you to get started, please read carefully, and sorry I couldn’t translate back to romanian, and to avoid any mistakes, I kept my code in english.
Project Model
Project History Model
Projects Controller:
projects.index Blade: in this blade, you can forloop thru all of your projects model, and assign them as
$project
, since we loaded the relationships earlier from the controller.You can easily access the relationships using
$project->histories
then assign each history model to$history
.Then you can go one step inside of the history relationship and call the inner relationship of
colaborator
with$history->colaborator
projects.show Blade: in this blade, we have a single project, and you can forloop thru all of your history models, since we loaded the relationships from the controller.
We assigned the histories collection as
$histories
then assign each history model to$history
Then you can go one step inside the history relationship and call the inner relationship of
colaborator
with$history->colaborator