Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anonymouse703's avatar

How to solve this? Undefined offset: 0 (View:)

When I dd($users) I get some data.

Collection {#336 ▼
  #items: array:1 [▼
    0 => User {#330 ▼
      #fillable: array:11 [▶]
      #hidden: array:2 [▶]
      #connection: "mysql"
      #table: "users"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:21 [▶]
      #original: array:21 [▼
        "id" => 1
        "first_name" => "Jerome"
        "last_name" => "Villaver"
        "middle_name" => "Llacuna"
        "achievement_id" => null
        "country_id" => 1
        "provider" => "system"
        "avatar" => "avatar.png"
        "avatar_url" => null
        "email" => "[email protected]"
        "status" => 0
        "email_verified_at" => null
        "password" => "ykyU8Rcw3ZroKD2UV3r.0uj.Yilar30EMTyZ5PvOd.IT6WEwYoZ4q"
        "remember_token" => null
        "availabity" => null
        "address" => null
        "educaction_id" => null
        "notes" => null
        "rating_id" => null
        "created_at" => "2019-02-18 00:44:59"
        "updated_at" => "2019-02-18 00:44:59"
      ]

and my view is something like this

@foreach($users as $user)

{{$user->last_name}},{{$user->first_name}},{{$user->middle_name}}

@endforeach
0 likes
9 replies
Nagraj722's avatar

Try below code

@foreach($users as $user)

{{$user->first()->last_name}},{{$user->first()->first_name}},{{$user->first()->middle_name}}

@endforeach

Nagraj722's avatar

Try to check with dd($user) what are the data getting

@foreach($users as $user)

dd($user)

@endforeach
Snapey's avatar

my partner changed some code

Try asking them first before wasting our time...

1 like
shez1983's avatar

@snapey no need to snap.. we have all experienced it - OP's problem is that he might be a beginner so doesnt know how to debug/find bugs etc.. do you ask your fellow colleagues everytime something stops working or debug it yourself?

1 like
Snapey's avatar

@shez1983 fair enough. I'm not bothered anyway because I did not spend anytime on this.

Sometimes I get a run of people that just dismiss your efforts and say 'already solved it'

The main issue I have here is that the OP did not post any relevant information about the error.

Please or to participate in this conversation.