lundi 7 mars 2016

Laravel Method Injection - Variable exists?

Let's say that we have Laravel's default User.php model, and we also have UsersController.php. Here's how the create method looks like:

public function create(User $user)
{
    return view('backend.users.form', compact('user'));
}

As you can see - User $user is passed as argument, and view backend.users.form receives that $user.

If I add dd($user); in the create method:

public function create(User $user)
{
    dd($user);
    return view('backend.users.form', compact('user'));
}

the result will be:

User {#193 ▼
  #fillable: array:3 [▼
    0 => "name"
    1 => "email"
    2 => "password"
  ]
  #hidden: array:2 [▼
    0 => "password"
    1 => "remember_token"
  ]
  #connection: null
  #table: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: []
  #original: []
  #relations: []
  #visible: []
  #appends: []
  #guarded: array:1 [▶]
  #dates: []
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: false
  +wasRecentlyCreated: false
}

So, variable (instance) $user exists, right?

But, in a view backend.users.form that receives $user, if we do something like this:

<h3>{!! $user->exists ? 'Editing '.$user->name : 'Create New User' !!}</h3>

The result will be as if $user does not exist? The result will be Create New User. I do not understand it. Can you explain to me why $user->exists returns false when dd($user); shows that it exists?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire