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:
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?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire