$employee = Employee::where('user_id', Auth::user()->id)->get();
foreach (Company::all() as $company)
{
if ($company->id == $employee[0]->company_id && $company->employee_active === 1)
{
$event->menu->add([
'text' => 'Contracten',
'url' => 'dashboard/contracts',
'icon' => 'file-text',
'submenu' => [
[
'text' => 'Contract opzetten',
'url' => 'dashboard/contracts/create',
'icon_color' => 'red',
]
]
]);
}
}
When I use this code I'm getting undefined offset: 0, if the database is empty. How can get this write? Should I use an if or something like that
Really you should configure your relationships correctly so that you can do
$companies = Company::all();
foreach($companies as $company){
foreach($company->employees as $employee){
if($employee->active){
....
}
}
}
But in your case you can change your first line to
$employee = Employee::where('user_id', Auth::user()->id)->first();
Which will return an Employee
object collection rather than an array so you don't need to use an [index]
to get the first object.