S
S
SteepZero2018-05-04 17:03:29
Laravel
SteepZero, 2018-05-04 17:03:29

How to set mock relations in Laravel?

I deal with tests in Laravel, I ran into a problem:
There is a model (model 1) - I can easily replace it with an instance from the factory
But this model has a hasMany-relation (model 2), it would also need to be replaced
How can this be organized?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2018-05-04
@SteepZero

class AlbumTest extends TestCase
{
    use DatabaseMigrations;

    public function test()
    {
        $album = factory('Album')->create();

        $songs = new Collection([
            factory('Song')->make(['duration' => 291]),
            factory('Song')->make(['duration' => 123]),
            factory('Song')->make(['duration' => 100]),
        ]);

        $album->songs()->saveMany($songs);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question