I am doing a test project for my learning of Laravel in its version 5.7
I am in the part of launching a seeder for, with everything configured in the factories, the models and the DatabaseSeeder, random data are created in the database to be able to test in development tasks.
Well, when executing the command to launch the seeder:
php artisan db:seed
I get this ERROR that I have no idea how to solve or give a solution:
As I say, I do not know how to solve the error and in the time that I have spent searching the Internet, I have not found anything about it.
The content of database/facories/CursoFactory.php
is this:
<?php
use App\Profesor;
use Faker\Generator as Faker;
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/
$factory->define(App\Curso::class, function (Faker $faker) {
////$profesores = Profesor::all();
/**
* Mejor que la anterior, poner como sigue:
* para que no se consulte y se cargue la lista de nuevo cada vez que se genere
* una instancia de Curso, lo que causaría lentitud en el proceso, se puede poner
* la variable a STATIC
*/
//static $profesores = Profesor::all();
/**
* y como a las variables STATIC no se les puede asignar, directamente, un valor,
* se deberá codificar de esta forma:
*/
static $profesores;
//Condicional ternario de no hacer nada si la variable ya tiene un valor (?vacío)
//y, sino (:), se le asigna uno
////$profesores ?: Profesor::all();
//o a través del ISSET, si es una variable ya establecida, su valor es igual a si mismo,
//sino, se le asigna un valor
isset($profesores) ? $profesores : Profesor::all();
return [
//sentence, para frases aleatorias || mt_ran(2,4) para generar un núm. de 2 a 4 palabras
'titulo' => $faker->sentence(mt_rand(2,4)),
//paragraph, generar párrafos(de 4 componentes, es decir, ¿4 líneas?)
'descripcion' => $faker->paragraph(4),
'profesor_id' => $profesores->random()->id,
];
});
The error occurs on the line of:
'profesor_id' => $profesores->random()->id,
But the assumed load of the variable $profesores
is something above, where, establishing it as static
, has been considered in two different ways, if the variable is loaded or not to give it if it is not, a Default value.
In some way, none of the conditionals is carried out and, therefore, the variable reaches the random()
with a null
thing that the random()
does not support and, therefore, the ERROR occurs.
Would anyone know what to do to avoid the failure shown?
Thanks. Greetings ...
EDITED
If I apply the solution proposed in the comment on replacing this
'profesor_id' => $profesores->random()->id,
for this
'profesor_id' => $profesores::random()->id,
then, I miss this other ERROR:
EDITED (2nd)
The problem statement may not have been understood. It is true that I put the error here, in this line:
'profesor_id' => $profesores->random()->id,
But, rather, I wanted to say that this is where the error finally jumps or is reflected. And the line that is pointed at the terminal.
But where the ERROR has the origin is in any of the two possible conditionals that I would like to use, that is, if I enable the load of the variable $profesores
by means of one of the two conditionals
either through
static $profesores;
$profesores ?: Profesor::all();
or through
static $profesores;
isset($profesores) ? $profesores : Profesor::all();
It is, then, when the ERROR of " Call to a member function random () on null " occurs, in the times that I enable any of the conditionals.
If I do not use the conditional load of the variable and assign it, on all occasions, the result of the query, the ERROR does not occur.
static $profesores;
$profesores = Profesor::all();
...
'profesor_id' => $profesores->random()->id,
But what I wanted was to control the load of the static variable $profesores
so that if it was already loaded, do nothing and if it was not, then pass the resulting collection of Profesor::all()
.
I hope that the problem is now clearer. Greetings.