Home > Software engineering >  How do i select items from a table(id) to display matched id from another table?
How do i select items from a table(id) to display matched id from another table?

Time:01-03

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.

img

@forelse ($istoric as $istProj)
         <div >
         <table class='table'>
            <tr >
                 <th>Id Proiect</th>
                 <th>Tip actiune </th>
                 <th>Colaborator </th>
                 <th>Suma </th>
                 <th>Data </th>
              </tr>
              <tr >
                <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 >
                 <h2>Nu au fost gasite inregistrari</h2>
               </div>
            @endforelse

CodePudding user response:

If you use model Collab, and within you have project relation , than you can use

Collab::query()
  ->with('project', function ($q) use ($id) {
    return $q->where('id', $id);
  })
  ->get();

Or you can use query builder as well

DB::table('collabs')
->select('collabs.*')
->join('projects', 'projects.id', '=', 'collabs.project_id')
->where('projects.id', $id)
->get();

Just adjust it according what you really need.

CodePudding user response:

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.

Caloboratori = Colaborators

Istoric Proiecte = Project History
id || auto_increment
project_id || bigInteger()
colaborator_id || bigInteger()

Proiecte = Project
id || auto_increment

Project Model

/* To load the history, we will be using hasMany relationship, because for each 
   project, we have lots of history, please read more on one-to-many relationships here
   https://laravel.com/docs/9.x/eloquent-relationships#one-to-many

    Istoric Proiecte = Project History
        id || auto_increment
        project_id || bigInteger()
        colaborator_id || bigInteger()
*/
public function histories() {
    return $this->hasMany(ProjectHistory::class);    
}

Project History Model

//We will reverse the one-to-many relationship, with belongsTo here. | example: project_id
public function project() {
    return $this->belongsTo(Project::class);
}

//We will reverse the one-to-many relationship, with belongsTo here. | example: colaborator_id
public function colaborator() {
    return $this->belongsTo(Colaborator::class);
}

Projects Controller:

// Show a list of all projects
public function index() {  
    //Get all projects
    $projects = Project::all();

    //Load all of the project relationships that we will be using
    $projects->load('histories.colaborator');
    
    return view('projects.index', compact('projects'));
 }

// Show a single project
public function show(Project $project) {  

    //Load all of the project relationships that we will be using
    $project->load('histories.colaborator');
    
    //Assign the loaded project history
    $histories = $project->histories;
    
    return view('projects.show', compact('project', 'histories'));
 }

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

@foreach ($projects as $project)
    <p>Project id: {{ $project->id }}
    <p>Project name: {{ $project->name }}

    <h1>Project History list</h1>
    @foreach ($project->histories as $history)
       <ul>
            <li>ID: {{$history->id}}</li>
            <li>Name: {{$history->name}}</li>
            <li>Colaborator Name: {{$history->colaborator->name}}</li> 
    </ul>
    @endforeach
@endforeach 

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

<p>Project name: {{ $project->name }}

<h1>Project History list</h1>
@foreach ($histories as $history)
    <ul>
        <li>ID: {{$history->id}}</li>
        <li>Name: {{$history->name}}</li>
        <li>Colaborator Name: {{$history->colaborator->name}}</li>
   </ul>
@endforeach 
  • Related