Laravel, When database is empty I'm getting error otherwise not but I have to use this
up vote
0
down vote
favorite
$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
laravel undefined offset
add a comment |
up vote
0
down vote
favorite
$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
laravel undefined offset
You should put all of your$company
logic in a simpleempty
checkif(!empty($employee)){ // then your logic of $employee[0] will make sense }
– Farooq Khan
Nov 9 at 8:06
Should I put if(!empty($employee)){ inside the foreach or outside?
– Bram Swinkels
Nov 9 at 8:09
what is you logic in here? if the company of the connected user hasemployee_active
attribute to 1 => add in menu ?
– N69S
Nov 9 at 8:31
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
$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
laravel undefined offset
$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
laravel undefined offset
laravel undefined offset
asked Nov 9 at 8:02
Bram Swinkels
32
32
You should put all of your$company
logic in a simpleempty
checkif(!empty($employee)){ // then your logic of $employee[0] will make sense }
– Farooq Khan
Nov 9 at 8:06
Should I put if(!empty($employee)){ inside the foreach or outside?
– Bram Swinkels
Nov 9 at 8:09
what is you logic in here? if the company of the connected user hasemployee_active
attribute to 1 => add in menu ?
– N69S
Nov 9 at 8:31
add a comment |
You should put all of your$company
logic in a simpleempty
checkif(!empty($employee)){ // then your logic of $employee[0] will make sense }
– Farooq Khan
Nov 9 at 8:06
Should I put if(!empty($employee)){ inside the foreach or outside?
– Bram Swinkels
Nov 9 at 8:09
what is you logic in here? if the company of the connected user hasemployee_active
attribute to 1 => add in menu ?
– N69S
Nov 9 at 8:31
You should put all of your
$company
logic in a simple empty
check if(!empty($employee)){ // then your logic of $employee[0] will make sense }
– Farooq Khan
Nov 9 at 8:06
You should put all of your
$company
logic in a simple empty
check if(!empty($employee)){ // then your logic of $employee[0] will make sense }
– Farooq Khan
Nov 9 at 8:06
Should I put if(!empty($employee)){ inside the foreach or outside?
– Bram Swinkels
Nov 9 at 8:09
Should I put if(!empty($employee)){ inside the foreach or outside?
– Bram Swinkels
Nov 9 at 8:09
what is you logic in here? if the company of the connected user has
employee_active
attribute to 1 => add in menu ?– N69S
Nov 9 at 8:31
what is you logic in here? if the company of the connected user has
employee_active
attribute to 1 => add in menu ?– N69S
Nov 9 at 8:31
add a comment |
3 Answers
3
active
oldest
votes
up vote
-1
down vote
accepted
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.
1
it's a bad solution to cycle through all the companies and then get all there employee.
– N69S
Nov 9 at 8:28
add a comment |
up vote
0
down vote
@Joe solution is correct. You can make an eager loading of companies and employees to avoid N+1 query problem. There is an official example of this case:
https://laravel.com/docs/5.7/eloquent-relationships#eager-loading
add a comment |
up vote
0
down vote
You should leverage constrained eager loading to limit your results. This will return only the companies where the user is an employee.
$companies = Company::where('employee_active', 1)->whereHas('employees', function($query) {
$query->where('user_id', auth()->id());
})->get();
Now you dont need the conditional checks for generating the menu items.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
-1
down vote
accepted
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.
1
it's a bad solution to cycle through all the companies and then get all there employee.
– N69S
Nov 9 at 8:28
add a comment |
up vote
-1
down vote
accepted
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.
1
it's a bad solution to cycle through all the companies and then get all there employee.
– N69S
Nov 9 at 8:28
add a comment |
up vote
-1
down vote
accepted
up vote
-1
down vote
accepted
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.
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.
answered Nov 9 at 8:14
Joe
2,55621627
2,55621627
1
it's a bad solution to cycle through all the companies and then get all there employee.
– N69S
Nov 9 at 8:28
add a comment |
1
it's a bad solution to cycle through all the companies and then get all there employee.
– N69S
Nov 9 at 8:28
1
1
it's a bad solution to cycle through all the companies and then get all there employee.
– N69S
Nov 9 at 8:28
it's a bad solution to cycle through all the companies and then get all there employee.
– N69S
Nov 9 at 8:28
add a comment |
up vote
0
down vote
@Joe solution is correct. You can make an eager loading of companies and employees to avoid N+1 query problem. There is an official example of this case:
https://laravel.com/docs/5.7/eloquent-relationships#eager-loading
add a comment |
up vote
0
down vote
@Joe solution is correct. You can make an eager loading of companies and employees to avoid N+1 query problem. There is an official example of this case:
https://laravel.com/docs/5.7/eloquent-relationships#eager-loading
add a comment |
up vote
0
down vote
up vote
0
down vote
@Joe solution is correct. You can make an eager loading of companies and employees to avoid N+1 query problem. There is an official example of this case:
https://laravel.com/docs/5.7/eloquent-relationships#eager-loading
@Joe solution is correct. You can make an eager loading of companies and employees to avoid N+1 query problem. There is an official example of this case:
https://laravel.com/docs/5.7/eloquent-relationships#eager-loading
answered Nov 9 at 8:45
Ivan
11
11
add a comment |
add a comment |
up vote
0
down vote
You should leverage constrained eager loading to limit your results. This will return only the companies where the user is an employee.
$companies = Company::where('employee_active', 1)->whereHas('employees', function($query) {
$query->where('user_id', auth()->id());
})->get();
Now you dont need the conditional checks for generating the menu items.
add a comment |
up vote
0
down vote
You should leverage constrained eager loading to limit your results. This will return only the companies where the user is an employee.
$companies = Company::where('employee_active', 1)->whereHas('employees', function($query) {
$query->where('user_id', auth()->id());
})->get();
Now you dont need the conditional checks for generating the menu items.
add a comment |
up vote
0
down vote
up vote
0
down vote
You should leverage constrained eager loading to limit your results. This will return only the companies where the user is an employee.
$companies = Company::where('employee_active', 1)->whereHas('employees', function($query) {
$query->where('user_id', auth()->id());
})->get();
Now you dont need the conditional checks for generating the menu items.
You should leverage constrained eager loading to limit your results. This will return only the companies where the user is an employee.
$companies = Company::where('employee_active', 1)->whereHas('employees', function($query) {
$query->where('user_id', auth()->id());
})->get();
Now you dont need the conditional checks for generating the menu items.
answered Nov 9 at 9:04
DigitalDrifter
6,5472423
6,5472423
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221865%2flaravel-when-database-is-empty-im-getting-error-otherwise-not-but-i-have-to-us%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You should put all of your
$company
logic in a simpleempty
checkif(!empty($employee)){ // then your logic of $employee[0] will make sense }
– Farooq Khan
Nov 9 at 8:06
Should I put if(!empty($employee)){ inside the foreach or outside?
– Bram Swinkels
Nov 9 at 8:09
what is you logic in here? if the company of the connected user has
employee_active
attribute to 1 => add in menu ?– N69S
Nov 9 at 8:31