Grab the right value when 2 tabel values are the same using Laravel

Viewed 32

Can someone help me with this problem?

I have 2 tables in phpMyAdmin

the first one where the with the department_id of the person

The second one where you can see the department_id and then the department_name

I have a table with this code:

<div class="container">        
    <table class="table table-striped table-bordered">
        <thead class="table-dark">
            <tr>
                <th scope="col">First Name</th>
                <th scope="col">Last Name</th>
                <th scope="col">Department Name</th>
            </tr>
        </thead>
        <tbody>
            @foreach ($employee as $name)
            <tr>
                <td>{{ $name->first_name }}</td>
                <td>{{ $name->last_name }}</td>
                <td>{{ $name->!!!IDK YET!!! }}</td>
            </tr>
            @endforeach
        </tbody>
    </table>
</div>

Basically, for each employee I want to grab the department_id and check what the matching department_name

Extra code that makes the table on my page work:

EmployeeController.php

<?php

namespace App\Http\Controllers;

use App\Models\Employee;
use Illuminate\Http\Request;

class EmployeeController extends Controller
{
    public function index()
    {
        $employee = Employee::all();
        return view('employee.index', compact('employee'));
    }
}

Employee.php (Model)

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    use HasFactory;
    protected $employees = 'employees';
    protected $EmployeeValues = ['first_name','last_name'];
    
    protected $departments = 'departments';
    protected $DepartmentValues = ['department_id','department_name'];
}
1 Answers

Okey so I found the answer after trying a lot of things.

My new code is:

EmployeeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class EmployeeController extends Controller
{
    public function index()
    {
        $employee = DB::table('employees')
            ->leftJoin('departments','employees.department_id',"=",'departments.department_id')
            ->get();
        return view('employee.index', compact('employee'));
    }
}

Employee (Model)

Wasn't necessary anymore

index.blade.php

<div class="container">        
    <table class="table table-striped table-bordered">
        <thead class="table-dark">
            <tr>
                <th scope="col">First Name</th>
                <th scope="col">Last Name</th>
                <th scope="col">Department Name</th>
            </tr>
        </thead>
        <tbody>
            @foreach ($employee as $name)
            <tr>
                <td>{{ $name->first_name }}</td>
                <td>{{ $name->last_name }}</td>
                <td>{{ $name->department_name }}</td>
            </tr>
            @endforeach
        </tbody>
    </table>
</div>
Related