What am I doing wrong? Arrrgggh Trying to get property of non-object

Posted 7 months ago by behnampmdg3
Trying to get property of non-object (View: /Users/f/codes/blog/resources/views/user.blade.php)

Route

Route::resource('/users', 'UserController');

UserController

public function show(User $user)
    {
        return view('user', ['user' => $user]);
    }

view

@foreach ($user as $u)
    <p>This is user {{ $u->id }}</p>
@endforeach 

dd($user) in controller prints:

User {#217 ▼
  #fillable: array:3 [▶]
  #hidden: array:2 [▶]
  #connection: "mysql"
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:8 [▶]
  #original: array:8 [▼
    "id" => 1
    "name" => "Buddy Hegmann"
    "email" => "[email protected]"
    "email_verified_at" => null
    "password" => "y$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm"
    "remember_token" => "7pOaOeTAlD"
    "created_at" => "2018-10-17 15:15:29"
    "updated_at" => "2018-10-17 15:15:29"
  ]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #visible: []
  #guarded: array:1 [▶]
  #rememberTokenName: "remember_token"
}

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.