Ad
Laravel, When Database Is Empty I'm Getting Error Otherwise Not But I Have To Use This
$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
Ad
Answer
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.
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS - Conditionally Load a Different Page
- → Make a Laravel collection into angular array (octobercms)
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
- → Validating fileupload(image Dimensions) in Backend Octobercms
- → OctoberCMS Fileupload completely destroys my backend
- → How do I call the value from another backed page form and use it on a component in OctoberCms
Ad