jeudi 29 mars 2018

Laravel 5.1 factory definition unable to resolve closure for foreign relationships

I'm having trouble defining the factory function to handle foreign relationships for my business model. This is the code for my business model factory. The error message that I am getting is :

Uncaught exception 'ErrorException' with message 'Object of class Closure could not be converted to string' in /Users/patricia/Code/thank-views/vendor/laravel/framework/src/Illuminate/Database/Connection.php:390 Stack trace

It seems that it is unable to resolve the id for the user. When I run it in tinker it creates the model with a closure function for those fields. However I want to be able to generate these models to be used for my BusinessTest class. It's unable to resolve these dependencies. I'm not sure whether the best place to resolve these dependencies should be in the ModelFactory or elsewhere in the codebase.

$factory->define(App\Business::class, function (Faker\Generator $faker) {
return [
    'slug' => $faker->word,
    'name' => $faker->name,
    'end_card' => $faker->word,
    'white_label' => $faker->boolean,
    'white_label_url' => $faker->word,
    'payment_header' => $faker->word,
    'payment_amount' => $faker->randomNumber(),
    'payment_amount_display' => $faker->word,
    'payment_cost' => $faker->randomNumber(),
    'payment_activated' => $faker->boolean,
    'main_user_id' => function () {
         return factory(App\User::class)->create()->id;
    },
];});



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire