laravel-seeding

How to track which seeders got applied in Laravel?

送分小仙女□ 提交于 2019-12-11 16:10:15
问题 The obvious way to apply specific seeds rather than seeding everything is by using the --class flag: php artisan db:seed --class[=CLASS] Is there a way to track which seeders have been applied so far in Laravel (say, the same way you can track how migrations got applied by running php artisan migrate:status )? Further, is there a way to apply a range of seeders (rather than specifying each class individually, which is very cumbersome). What made me think about this is this part in a seeder in

Seeding pivot table with Laravel 5.5 model factory - mb_strtolower() expects parameter 1 to be string, array given

杀马特。学长 韩版系。学妹 提交于 2019-12-11 06:18:00
问题 Given that I have the following tables: users questions tags question_tag my pivot table with two fields: question_id & tag_id and my App\Question model has the following relationships: class Question extends Model { public function user() { return $this->belongsTo(User::class); } public function tags() { return $this->hasMany(Tag::class); } } I've created the following factories: database/factories/UserFactory.php $factory->define(App\User::class, function (Faker $faker) { static $password;

Seed a recursive table using factories in Laravel

南楼画角 提交于 2019-12-11 05:53:41
问题 I have a recursive table in my Laravel DB project that handles a dynamic menu. For testing purposes I want to seed that table with random elements. A random element of faker library will be okay for the menu name. The problem is how to generate the parent_id numbers that match with any element of that menu table. I'll show you an example: +---------------------------+ | id | name | parent_id | +---------------------------+ | 1 | animals | 0 | |---------------------------| | 2 | food | 0 | |--

Seeding many-to-many relationship with model factory in laravel - column cannot be null error

懵懂的女人 提交于 2019-12-11 04:39:02
问题 Given that I have the following tables: users questions tags question_tag my pivot table with two fields: question_id & tag_id and these are my model relationships: User public function questions() { return $this->hasMany(Question::class); } Question public function user() { return $this->belongsTo(User::class); } public function tags() { return $this->belongsToMany(Tag::class); } Tag public function questions() { return $this->belongsToMany(Question::class); } I have setup the database model

Laravel 5.6 db:seed throws FatalThrowableError : Class 'APP\Todo' not found

对着背影说爱祢 提交于 2019-12-11 02:36:41
问题 i am using faker generate to generate fake entries and insert it into database with the help of php artisan db:seed when i run this command it shows an error : Seeding: TodosTableSeeder Symfony\Component\Debug\Exception\FatalThrowableError : Class 'APP\Todo' not found at /var/www/html/todos/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:217 213| if ($this->amount < 1) { 214| return (new $this->class)->newCollection(); 215| } 216| 217| $instances = (new $this-

How to implement your own Faker provider in Laravel

ぃ、小莉子 提交于 2019-12-09 09:46:42
问题 I want to create a custom provider for Faker in Laravel (e.g. one for a random building name). Where do I store the custom provider in my application and how do I use it? 回答1: You should use php artisan to generate the custom provider... On the command line, navigate to the root of your app and type... php artisan make:provider FakerServiceProvider That should generate a new provider in the app/Providers folder. Here is what my register function looks like going off the example in the faker

Laravel timestamps() doesn't create CURRENT_TIMESTAMP

可紊 提交于 2019-12-05 16:49:08
问题 I have a migration that has the timestamps() method, and then I have a seed to seed this table. Schema::create('mytable', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->timestamps(); }); The seed looks like this: DB::table('mytable')->insert([ [ 'title' => 'My Awesome Title' ] ]); When it all gets run using: php artisan migrate:refresh --seed The item gets inserted, but the values of created_at and updated_at are both 0000-00-00 00:00:00 why are they

Laravel timestamps() doesn't create CURRENT_TIMESTAMP

◇◆丶佛笑我妖孽 提交于 2019-12-04 00:48:07
I have a migration that has the timestamps() method, and then I have a seed to seed this table. Schema::create('mytable', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->timestamps(); }); The seed looks like this: DB::table('mytable')->insert([ [ 'title' => 'My Awesome Title' ] ]); When it all gets run using: php artisan migrate:refresh --seed The item gets inserted, but the values of created_at and updated_at are both 0000-00-00 00:00:00 why are they not set correctly? here are the column schemes that it creates: `created_at` TIMESTAMP NOT NULL DEFAULT

How to pass arguments to Laravel factories?

浪子不回头ぞ 提交于 2019-12-03 15:29:37
问题 I have a users table and a one-to-zero/one relation with a businesses table (users.user_id => businesses.user_id). On my users table I have a discriminator which tells me if the user is of type business and therefore I need to have details on the businesses table as well. I want to create my Users with my factory which currently is working and then only create business details where the discriminator points to a business account. I have three options in my mind: Create from users factory and

How to implement your own Faker provider in Laravel

你离开我真会死。 提交于 2019-12-03 12:35:15
I want to create a custom provider for Faker in Laravel (e.g. one for a random building name). Where do I store the custom provider in my application and how do I use it? You should use php artisan to generate the custom provider... On the command line, navigate to the root of your app and type... php artisan make:provider FakerServiceProvider That should generate a new provider in the app/Providers folder. Here is what my register function looks like going off the example in the faker docs. /** * Register the application services. * * @return void */ public function register() { $this->app-