jeudi 30 juin 2016

Empty Object being passed to route

I am trying to work out why an empty object is being passed through my route. I have a user model

class User extends Model
{
    protected $table = 'users';
    protected $guarded = [];

    public function userObjectives()
    {
        return $this->hasMany('App\UserObjectives');
    }
}

And I have a UserObjectives model

class UserObjectives extends Model
{
    protected $table = 'user_objectives';
    protected $guarded = [];

    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

I then have an index controller which displays the default page. The index function gets the logged in User and passes it to the view

public function index()
{
    $loggedInUser = Auth::user()->getFirstName() . ' '  . Auth::user()->getLastName();
    $user = User::where('userName', '=', $loggedInUser)->first();

    if($user) {
        return view('index', compact('user'));
    } else {
        return view('auth.login');
    }
}

I am doing it like the above because I am mapping users from Active Directory. Within my index view, if I do the following I can see the details for the logged in user.



Within the index view, I have a link to a route which I pass the user object

{!! link_to_route('users.userObjectives.index', 'My Info', array($user), array('class' => 'btn btn-info')) !!}

Now within the UserObjectives Controller I have

public function index(User $user)
{
    dd($user);
}

Now for some reason the above displays an empty User Object. If I change it to the following, without User, I get the id of the logged in user.

public function index($user)
{
    dd($user);
}

Why would this function display an empty User Object even though I can see within the view that the User object I am passing does have data?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire