There is a table with fields:

$table->increments('id'); $table->string('title'); $table->string('meta_description'); $table->string('meta_keywords'); $table->string('image'); $table->string('description'); $table->string('author'); $table->timestamps(); 

How do I now create a seed to insert a record? I do like this, nothing happens:

 <?php use Illuminate\Database\Seeder; class blog_table_seeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { DB::table('blog')->insert([ 'id' => NULL, // Π—Π΄Π΅ΡΡŒ Π°Π²Ρ‚ΠΎΠΈΠ½ΠΊΡ€Π΅ΠΌΠ΅Π½Ρ‚ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π±Ρ‹Ρ‚ΡŒ 'title' => 'Ρ‚Π°ΠΉΡ‚Π» новости', 'meta_description' => 'ΠΌΠ΅Ρ‚Π° описаниС', 'meta_keywords' => 'ΠΊΠ»ΡŽΡ‡Π΅Π²ΠΈΠΊΠΈ', 'image' => 'ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ°', 'description' => 'ОписаниС', 'author' => 'Автор' // Π”Π°Ρ‚Π° создания, я Ρ‚Π°ΠΊ понял, Π°Π²Ρ‚ΠΎΠΌΠ°Ρ‚ΠΎΠΌ заполняСтся? ]); } } 

    2 answers 2

     'id' => NULL ΡƒΠΊΠ°Π·Ρ‹Π²Ρ‚ΡŒ Π½Π΅ Π½Π°Π΄ΠΎ. 

    Running migrations on command:

     php artisan db:seed 

    or

     php artisan db:seed --class=blog_table_seeder 

    and recommend classes to call so BlogTableSeeder

    • If you run through php artisan db:seed , then you need to write such a thing in the DatabaseSeeder class in the run() method: $this->call('blog_table_seeder'); - Konstantin
    • Yes, important addition, thank you. - Butochnikov
    • Everything works, but tell me how to make it so that the current date is automatically substituted? - Onotol
    • If laravel-way, then Carbon::now() . Documentation here: github.com/briannesbitt/Carbon - Butochnikov

    First, you do not need to pass id. It will be substituted automatically.

    Secondly, the side file, as well as the class, should be called CamelCase in the format.
    The file name must match the class name.

    Thirdly, if you created the cider manually, execute the command in the console

     composer dump-autoload 

    being in the project folder.

    Do not forget to migrate the team

     php artisan migrate 

    To sit the database run the command

     php artisan db:seed --class=blog_table_seeder 

    To your data from your seder hit the database without specifying the class on the command

     php artisan db:seed 

    It is necessary that the folder DatabaseSeedeer.php following content be in the list of servers:

     <?php use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { // Для Laravel 5.1 $this->call(blog_table_seeder::class); // Для Laravel 4.2 Π·Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ Π½Π° // $this->call('blog_table_seeder'); } } 

    The insert method does not automatically insert date.

    There are two solutions.

    The first solution is to insert data in the sider:

     <?php use Illuminate\Database\Seeder; class blog_table_seeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { DB::table('blog')->insert([ 'title' => 'Ρ‚Π°ΠΉΡ‚Π» новости', 'meta_description' => 'ΠΌΠ΅Ρ‚Π° описаниС', 'meta_keywords' => 'ΠΊΠ»ΡŽΡ‡Π΅Π²ΠΈΠΊΠΈ', 'image' => 'ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ°', 'description' => 'ОписаниС', 'author' => 'Автор', // заполняСм timestamps Π²Ρ€ΡƒΡ‡Π½ΡƒΡŽ 'created_at' => date('Ymd H:i:s'), 'updated_at' => date('Ymd H:i:s') ]); } } 

    The second solution is through adding and using the model:

    Blog.php

     <?php // На этом этапС Π±ΡƒΠ΄ΡŒΡ‚Π΅ Π²Π½ΠΈΠΌΠ°Ρ‚Π΅Π»ΡŒΠ½Π΅Π΅ с namespace class Blog extends Eloquent { protected $table = 'blog'; } 

    Π’Π°Ρˆ сидСр.php

     <?php use Illuminate\Database\Seeder; // На этом этапС Π±ΡƒΠ΄ΡŒΡ‚Π΅ Π²Π½ΠΈΠΌΠ°Ρ‚Π΅Π»ΡŒΠ½Π΅Π΅ с namespace class blog_table_seeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { // ΠΌΠ΅Ρ‚ΠΎΠ΄ create автоматичСски Π·Π°ΠΏΠΎΠ»Π½ΠΈΡ‚ timestamps Blog::create([ 'title' => 'Ρ‚Π°ΠΉΡ‚Π» новости', 'meta_description' => 'ΠΌΠ΅Ρ‚Π° описаниС', 'meta_keywords' => 'ΠΊΠ»ΡŽΡ‡Π΅Π²ΠΈΠΊΠΈ', 'image' => 'ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ°', 'description' => 'ОписаниС', 'author' => 'Автор' ]); } }