Laravel seeder doesn't seed password - php

I want to seed users to database but seeder doesn't create password.
I have This migration File
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('email')->unique();
$table->string('username');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}
and this Seeder file
// Composer: "fzaninotto/faker": "v1.3.0"
use Faker\Factory as Faker;
class UsersTableSeeder extends Seeder {
public function run()
{
$faker = Faker::create();
DB::table('users')->truncate();
foreach(range(1, 3) as $index)
{
$user = User::create(array(
'password' => $faker->word,
'username' => $faker->userName,
'email' => $faker->email
));
}
}
}
I did php artisan migrate and it said "nothing to migrate"
so now when I do php artisan db:seed --class=UsersTableSeeder it throws no error.
Any idea why seeder does not create username? I have just empty column in my database...
thank you very much for any help.

Try adding Eloquent::unguard(); before the start of the foreach() loop.
Also, migration (php artisan migrate command) is only concerned with running the migrations (creating the database not populating).
Alternately you can include on your DatabaseSeeder $this->call('UsersTableSeeder'); then you can run php artisan migrate --seed

Related

Laravel Seeder not found

I'm running a few seeders during a migration after creating a table. Here's my migration file create_institutions_table
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateInstitutionsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('institutions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('code');
$table->timestamps();
$table->softDeletes();
});
$seeder = new InstitutionsSeeder();
$seeder->run();
$seeder2 = new UsersSeeder();
$seeder2->run();
Schema::table('users', function (Blueprint $table) {
$table->foreign('institution_id')->references('id')->on('institutions');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('institutions');
}
}
here's the InstitutionsSeeder
use Illuminate\Database\Seeder;
class InstitutionsSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
DB::table('institutions')->insert([
'name' => 'Institution One',
'code' => 'I1',
]);
}
}
here's the UsersSeeder
use Illuminate\Database\Seeder;
class UsersSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
DB::table('users')->insert([
'first_name' => 'Admin',
'last_name' => 'Istrator',
'email' => 'admin#example.com',
'institution_id' => '1',
'password' => '$2y$10$/wYQaaaaaaagrtyh64gbdt4yuhr32l4VmFHI.sINMR/9LXsj1MTy',
]);
}
}
As far as I can tell there's no real difference between the seeders, but the migration fails when trying to instanciate the UsersSeeder class while the InstitutionsSeeder works fine. This is the exception I'm getting from the php artisan migrate:fresh command:
Symfony\Component\Debug\Exception\FatalThrowableError : Class 'UsersSeeder' not found
at H:\code\MyProject\database\migrations\2019_06_17_224612_create_institutions_table.php:27
23|
24| $seeder = new InstitutionsSeeder();
25| $seeder->run();
26|
> 27| $seeder2 = new UsersSeeder();
28| $seeder2->run();
29|
30| Schema::table('users', function (Blueprint $table) {
31| $table->foreign('institution_id')->references('id')->on('institutions');
Exception trace:
1 CreateInstitutionsTable::up()
H:\code\MyProject\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:379
2 Illuminate\Database\Migrations\Migrator::Illuminate\Database\Migrations\{closure}()
H:\code\MyProject\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:388
Please use the argument -v to see more details.
Why won't the UsersSeeder work?
Two possible solutions:
Check the namespace of your class
Run composer dump-autoload: composer dump-autoload (You can read the docs here)
Each time you create a new seeder run composer dump-autoload command.
After that just run the seeder using php artisan db:seed command.
Hope this will work !

When I run: php artisan db:seed. I get his output: Cannot access empty property

UsersTapleSeeder.php :
<?php
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
factory('App\User', 50)->create();
}
}
DatabaseSeeder.php:
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
protected $toTruncate = ['users'];
public function run()
{
foreach ($this-> $toTruncate as $table)
{
DB::table('users')->truncate();
}
$this->call(UsersTableSeeder::class);
}
}
ModelFactory.php:
<?php
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/
/** #var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(App\User::class, function (Faker\Generator $faker) {
static $password;
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'body' => $faker->sentences(),
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
];
});
My users table:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->text('body');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
I tried migrate, migrate rollback, composer dump-autoload.
It was working until I added the body column, after that even when I delete it.
$this-> $toTruncate in your seeder is causing that error. Change it to:
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
protected $toTruncate = ['users'];
public function run()
{
foreach ($this->toTruncate as $table)
{
DB::table($table)->truncate();
}
$this->call(UsersTableSeeder::class);
}
}

Laravel 5: php artisan migrate:refresh

I am working on a laravel project and each time I change my table (add or remove column) and run php artisan migrate:refresh. I get this error:
[Symfony\Component\Debug\Exception\FatalErrorException] Can't use
method return value in write context
Solution tried:
run composer dump-autoload (Fails)
Drop table in database, delete the migration file and restart again (works)
Previous migration file:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_id');
$table->string('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
Changed migration file:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('post_id');
$table->string('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
I added the user_id in the change file in the up function
Try this command it works for me
php artisan migrate:fresh
However, be careful! This command will drop all data from your DB:
Note: The migrate:fresh command will drop all tables from the database and then execute the migrate command.
as per Laravel docs 9.x.
try this
fire this command
php artisan make:migration add_user_id_to_comments_table --table=comments
this will create a new migration file then
$table->integer('user_id')->after('id');
then use
php artisan migrate
Refresh the database and run all database seeds...
php artisan migrate:fresh --seed
Read Docs

Database Seeding in Laravel 5.1

I am using laravel 5.1. I am trying to run database seeding command.
My table name is users
My migration file is as below
2015_11_09_194832_create_users_table.php
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}
DatabaseSeeder.php
<?php
use App\User;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Model::unguard();
$this->call(UsersTableSeeder::class);
Model::reguard();
}
}
UsersTableSeeder.php
<?php
// DatabaseSeeder.php
use App\User;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Model::unguard();
DB::table('users');
$users = array(
['name' => 'Ryan Chenkie', 'email' => 'ryanchenkie#gmail.com', 'password' => Hash::make('secret')],
['name' => 'Chris Sevilleja', 'email' => 'chris#scotch.io', 'password' => Hash::make('secret')],
['name' => 'Holly Lloyd', 'email' => 'holly#scotch.io', 'password' => Hash::make('secret')],
['name' => 'Adnan Kukic', 'email' => 'adnan#scotch.io', 'password' => Hash::make('secret')],
);
// Loop through each user above and create the record for them in the database
foreach ($users as $user)
{
User::create($user);
}
Model::reguard();
}
}
While I am trying to run seeding command php artisan db:seed I am getting below error.
[ReflectionException]
Class UsersTableSeeder does not exist
Can anyone help me in this regard ??
I've run in to this issue a few time when I've added seeders either in a new project or to an existing project where I want to add some data to test.
Both jedrzej.kurylo and Alex were on the right track, composer dump-autoload which will regenerate your autoload files and include the seeder(s) you've just added.

Laravel migrations error

When I run php artisan migrate i get error B
Base table or view already exists: 1050 Table 'categories' already
exists'
What is it? Why? How to find error?
My categories migrations file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CategoriesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->index();
$table->text('description');
$table->integer('attachment_id')->unsigned()->index();
$table->foreign('attachment_id')->references('id')->on('attachment')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
In that migration file, the function drop() should be
public function down()
{
Schema::drop('categories');
}
It seems that you try to create existing table..
so i think you are editing the migration file that you already installed
you can drop your database and re-migrate it
php artisan migrate:reset
then
php artisan migrate

Categories