lundi 27 mars 2017

Cannot access eloquent relationship in laravel test

I have the following relationship in User model and UserLevel model.

User:

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

UserLevel:

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

This works perfectly in Tinker as shown below:

enter image description here

But I can't seem to access the relationship in Laravel PHPUnit test. The following die and dump returns null:

class AddUserTest extends TestCase
{
  use DatabaseMigrations;

  /** @test */
  public function super_admin_can_view_add_user_form()
  {
    $super_admin_user = factory(User::class)->create([
      'username' => 'Test User 6',
      'user_level_id' => 7,
    ]);

    dd($super_admin_user->user_level);
  }
}

If I dd the $super_admin_user, it properly shows the created user. What am I doing wrong? How can I access the user_level in the test?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2nEOAFq
via IFTTT

Aucun commentaire:

Enregistrer un commentaire