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

Mona_Salih12's avatar

show data from different table

How can I retrieve (first name, last name) from the trainee table and (student status, company, start date, end date) from the EmployeeLog table, ensuring that all the data matches the academy_id and cohort_id?

"academy_id" and "cohort_id" are sent from welcome.blade.php.

<td><a class="btn btn-primary btn-sm" href="{{ route('traineeLog.index', ['academy_id' => $info['academy_id'],'cohort_id' => $info['Cohort']]) }}">Trainees Log</a></td>

web.php

Route::get('/traineLog/{academy_id}/{cohort_id}', [TraineeController::class, 'traineeLog'])->name('traineeLog.index');

TraineeController.php

     public function traineeLog($academy_id,$cohort_id)
{        
   
     //
How can I retrieve data from both the trainee table and the employeeLog table, and then send it for viewing?
        
    return view('employment-status.employment_log_status',compact('data'));

}
0 likes
13 replies
tykus's avatar

JOIN the tables using whatever FK links the data (something like the following - since we don't have all of the information necessary)

public function traineeLog($academy_id,$cohort_id)
{        
    $data = DB::table('trainees')
        ->select('first_name', 'last_name', 'status', 'company', 'start_date', 'end_date')
        ->leftJoin('employee_log', 'trainees.column', '=', 'employee_log.column')
        ->where('academy_id', $academy_id)
        ->where('cohort_id', $cohort_id)
        ->get();
        
    return view('employment-status.employment_log_status',compact('data'));

}
Snapey's avatar

create a eloquent relationship descibing the relationship between the two tables

Mona_Salih12's avatar

@Snapey

Thank you for your response. Could you please provide a link for instructions on how to accomplish this?

gych's avatar

@Mona_Salih12

Laracasts has a series on this but its only for paid subscribers: https://laracasts.com/series/eloquent-relationships

There's also a free episode available in the Laravel 8 from scratch series which should get you started: https://laracasts.com/series/laravel-8-from-scratch/episodes/19

You can then read the Laravel documentation for the full details on eloquent relationships: https://laravel.com/docs/10.x/eloquent

If you do a quick google search you'll find multiple tutorials and youtube videos.

1 like
Mona_Salih12's avatar

@gych

To be honest, I tried to create one, but it didn't work for me. Thank you very much for your answer. I will check them all.

gych's avatar

@Mona_Salih12 No problem I'm sure that following this will point you in the right direction. If you get stuck or have any more questions, don't hesitate to reach out.

1 like
Mona_Salih12's avatar

Thank you for all the valuable advice. This is my first experience working on the backend, especially with Laravel, and I find it quite enjoyable. With a little patience, I managed to solve the issue. Here's how I did it: TraineeController Controller

 public function traineeLog($academy_id,$cohort_id)
{   
        
    $trainees = Trainee::where('academy_id', $academy_id)
    ->where('cohort_id', $cohort_id)
    ->with(['employment_logs' => function ($query)use ($academy_id,$cohort_id) {
        
        $query->where('status', 'Internship')->where('academy_id', $academy_id)
        ->where('cohort_id', $cohort_id);
    }])
    ->get();
        return view('employment-status.employment_log_status',compact('trainees'));
    }

trainees.blade.php

 @foreach ($trainees as $trainee)
                      <tr>
                          <td data-label="ID">{{ $trainee->first_name }}</td>
                          <td data-label="ID">{{ $trainee->last_name }}</td>
                          @foreach ($trainee->employment_logs as $log)
                          <td data-label="ID">{{$log->status }}</td>
                          <td data-label="ID">{{$log->company }}</td>
                          <td data-label="ID">{{$log->start_date }}</td>
                          <td data-label="ID">{{$log->end_date }}</td>
                          <td data-label="ID">{{$log->academy_id}}</td>
                          <td data-label="ID">{{$log->cohort_id}}</td>
                          @endforeach
                         </tr>
                  @endforeach

tykus's avatar

@Mona_Salih12 what happens to your table when the Trainee's each have different numbers of employment_logs results - you will have different numbers of columns on each row???

Mona_Salih12's avatar

@tykus In my code, I have implemented a functionality where it returns all trainees' (first name, last name) and their respective employment logs' (status, company, start date, end date) based on the provided academy id and cohort id. Additionally, each student may have different employment logs. The code specifically returns trainees whose employment status is set to "internship".

I hope this clarifies your inquiry.

amitsolanki24_'s avatar

@mona_salih12 for joining more than one table we use joins.

    $data = DB::table('trainees')
        ->select('first_name', 'last_name', 'status', 'company', 'start_date', 'end_date')
        ->join('employee_log', 'trainees.id', '=', 'employee_log.trainee_id')
        ->where([
            'academy_id' => $academy_id,
            'cohort_id' => $cohort_id
        ])
        ->get();

traninees.id and employee_log.trainee_id column replace these both column name with your actual database column name

Please or to participate in this conversation.