Doubt to create a ModelFactory Laravel 5.3


Knowing that I have 3 tables:

people (id, name, surname)

$factory->define(SIMante\Personas::class, function ($faker) {
    return [
        'nombres' => $faker->firstName,
        'apellidos' => $faker->lastName,

profiles (id, name)

$factory->define(SIMante\Perfiles::class, function ($faker) {
    return [
        'nombre' => $faker->unique()->randomElement($array = array ('Administrador','Usuario','Operario')),

users (id, id_personas (unique), name, mail, password, id_profiles)

I do not understand how to create the factory for the table since the column id_personas must be unique for each user.

here what I wear:

$factory->define(SIMante\User::class, function (Faker\Generator $faker) {
    return [
        'id_personas' => $faker->unique()->randomElement(SIMante\Perfiles::all()->id),
        'nombre' => $faker->unique()->userName,
        'correo' => $faker->unique()->safeEmail,
        'clave' => bcrypt('123456'),
        'id_perfiles' => SIMante\Perfiles::all()->random()->id,

but that throws me the following error


undefined property: illuminate \ database \ eloquent \ collection :: $ id

I have tried in several ways, but they all give me an error, although that is the one I see the most logic to use.

asked by Pablo Contreras 22.11.2016 в 05:03

1 answer


You could try using the pluck method to get only the value of the id, as you are doing it is not possible, because you have a collection (which is a kind of array) and you are calling a property in the array, which does not exist .

'id_personas' => $faker->unique()->randomElement(SIMante\Personas::all()->pluck('id')->toArray())
answered by 22.11.2016 / 05:13