-1

I have a page where each user has their profile that they can browse and see their created pages called "builds". I have this user, userA, without any builds that is getting the following exception as soon as you go to his profile:

ErrorException (E_ERROR)
Undefined variable: build (View: /var/www/html/example/resources/views/profiles/show.blade.php)
Previous exceptions
Undefined variable: build (0)

When I look him up using tinker the user doesn't have any builds:

>>> $user = App\User::where('username', 'userA')->first();
=> App\User {#3073
     id: 731,
     username: "userA",
     email: "email@example.com",
     email_verified_at: "2019-11-15 16:22:03",
     created_at: "2019-11-10 18:31:54",
     updated_at: "2019-11-15 16:22:03",
   }
>>> $user->builds;
=> Illuminate\Database\Eloquent\Collection {#3078
     all: [],
   }
>>>

I have another user, userB, also without any builds that is working just fine when you go to his profile, no exception is shown. This is the data that tinke returns for userB:

>>> $user = App\User::where('username', 'userB')->first();
=> App\User {#3066
     id: 1,
     username: "userB",
     email: "email@example.com",
     email_verified_at: "2019-10-08 20:51:32",
     created_at: "2019-10-03 00:44:02",
     updated_at: "2019-10-08 20:51:32",
   }
>>> $user->builds;
=> Illuminate\Database\Eloquent\Collection {#3061
     all: [],
   }
>>>

So I have 2 users, without any builds. You can view the profile of userB but userA throws an exception for some reason. In my view I made sure to check if there are any builds for the user by using:

@if($builds->isEmpty())
    User has not created any builds.
@else
    @foreach...

I have not experienced this for weeks when a user all of a sudden reported to me today that they are not able to view their profile. Does someone know what is causing this?

Muki
  • 51
  • 8
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – fonini Nov 18 '19 at 18:45
  • @fonini nope not really since all profiles work, those with or without builds, but only this one profile throws the exception. – Muki Nov 18 '19 at 18:51
  • I'd check isset() before the isEmpty()... If it fails isset() there are no builds to worry about – Clint Nov 18 '19 at 18:56
  • 2
    Show us the code where you use `$build`. You've showed `$builds`, but that's not the same. – ceejayoz Nov 18 '19 at 19:01
  • @ceejayoz I defined `$build` in the for loop like this `@foreach($builds as $build)` I don't think there is anything wrong with the code since ALL other profiles work just fine, with our without any builds, but this particular one doesn't work. – Muki Nov 18 '19 at 19:22
  • show the code ... @Muki your assumptions are not correct otherwise you wouldn't be here :) – lagbox Nov 18 '19 at 23:20

1 Answers1

0

Perhaps you are not binding the model to your template? are you returning something like I'm guessing there some weird binding issue going on here

public function getUser ()
{
$user = User::where('username', 'userB')->first();

return view ('index')->with('user',$user);
}