How to use factory to generate data with different models?

Posted 10 months ago by Edris899

Hi there,

Using Laravel 5.6.

I am making a illustration & album website. And I have already generated with factory some albums.

I have a couple of questions.

Question 1 How can I generate photos that are linked to those generated albums at that specific time. I know how to generate it separately but this one I don't know. I have already a photo model etc.

Question 2 I am using a categories package for laravel, is there also a way to give each generated photo a random categorie?

Question 3 Is is possible to give each photos src a random link to a dummy picture? And how?

Thanks in advance

This is the faker code for the album.

<?php

use Faker\Generator as Faker;

$factory->define(App\Album::class, function (Faker $faker) {
    return [
        'name'          => $faker->word,
        'description'   => $faker->sentence(),
        'user_id'       => 2,
    ];
});

This is the faker code for the photo.

<?php

use Faker\Generator as Faker;

$factory->define(App\Photo::class, function (Faker $faker) {
    return [
        'user_id'       =>  2,  
        'title'         =>  $faker->word,
        'description'   => $faker->sentence(),
        

    ];
});

Please sign in or create an account to participate in this conversation.