I have two MySQL tables in my Laravel-application, one called categories and the other called employees. The structure of the categories-table is:
id
category
order
and the employees table also has columns called:
id
category
order
So, lets say I have categories like: Consultants, Programmers and Administration and when I create an Employee in the backend I can assign the new employee to one of these categories. Now in the frontend of my Laravel-application I want the Employees displayed by the categories, and also the categories by order they are given. Let's say Consultants has order of 2, Programmers order of 1 and Administration order of 3.
Right now my controller looks like this:
use App\Employee;
class EmployeesController extends Controller
{
public function index()
{
$employees = Employee::all()->groupBy('category');
return view('app.employee.index', compact('employees'));
}
}
and my blade view file:
@foreach ($employees as $category => $workers)
<div class="col text-center mb-6">
<h2>{{ $category }}</h2>
</div>
<div class="row px-4 px-xl-10">
@foreach($workers->sortBy('order') as $worker)
// content of the employee
@endforeach
</div>
@endforeach
This sorts the employees correctly by simply using the categories of the Employees-table but with this I'm not able to sort by categories like I want to as described above.
So, can someone help me out?
EDIT
As an example I want the output look like this:
Programmers (since this category has order of 1)
// employees with category "programmers" here
Consultants (2)
// employees with category "consultants" here
Administration (3)
// employees with category "administration" here
orderused for sortingEmployeesis from Employee table orcategoriestable?