get data Factory Relationships for Laravel Testing


according laravel-database-testing

I have a factory relationship for testing data, my code like this

 public function test_users_can_authenticate_using_the_login_screen()
        $data = Organization::factory()
            ->has(User::factory()->count(2), 'users')

        $response = $this->post('/login', [
            'email' => $data->email,
            'password' => 'password',


How do I get the user->email data??, when i try $data->email or $data->users are not found?

>Solution :

Have you add the relationship for both models (Organization & User)?

If yes, then you need to check specific which user you want to grab. Since it returns array of users.

try this for example

$data = Organization::factory()
            ->has(User::factory()->count(2), 'users')
$firstUserEmail = $data->users[0]->email;

Leave a ReplyCancel reply