php artisan db:seed is not working Laravel 5 - php

I have read other answers but nothing solved my issue. When I run php artisan db:seed nothing happens, even no error is thrown.This is my DatabaseSeeder class
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Model::unguard();
// call our class and run our seeds
$this->call(BearAppSeeder::class);
// $this->call(UserTableSeeder::class);
Model::reguard();
}
}
BearAppSeeder contains following content.
<?php
use Illuminate\Database\Seeder;
class BearAppSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
// clear our database ------------------------------------------
/*DB::table('bears')->delete();
DB::table('fish')->delete();
DB::table('picnics')->delete();
DB::table('trees')->delete();
DB::table('bears_picnics')->delete();*/
// seed our bears table -----------------------
// we'll create three different bears
// bear 1 is named Lawly. She is extremely dangerous. Especially when hungry.
$bearLawly = Bear::insert(['name' => 'Lawly',
'type' => 'Grizzly',
'danger_level' => 8]);
/*/ bear 2 is named Cerms. He has a loud growl but is pretty much harmless.
$bearCerms = Bear::create(array(
'name' => 'Cerms',
'type' => 'Black',
'danger_level' => 4
));
// bear 3 is named Adobot. He is a polar bear. He drinks vodka.
$bearAdobot = Bear::create(array(
'name' => 'Adobot',
'type' => 'Polar',
'danger_level' => 3
));
$this->command->info('The bears are alive!');
// seed our fish table ------------------------
// our fish wont have names... because theyre going to be eaten
// we will use the variables we used to create the bears to get their id
Fish::create(array(
'weight' => 5,
'bear_id' => $bearLawly->id
));
Fish::create(array(
'weight' => 12,
'bear_id' => $bearCerms->id
));
Fish::create(array(
'weight' => 4,
'bear_id' => $bearAdobot->id
));
$this->command->info('They are eating fish!');
// seed our trees table ---------------------
Tree::create(array(
'type' => 'Redwood',
'age' => 500,
'bear_id' => $bearLawly->id
));
Tree::create(array(
'type' => 'Oak',
'age' => 400,
'bear_id' => $bearLawly->id
));
$this->command->info('Climb bears! Be free!');
// seed our picnics table ---------------------
// we will create one picnic and apply all bears to this one picnic
$picnicYellowstone = Picnic::create(array(
'name' => 'Yellowstone',
'taste_level' => 6
));
$picnicGrandCanyon = Picnic::create(array(
'name' => 'Grand Canyon',
'taste_level' => 5
));
// link our bears to picnics ---------------------
// for our purposes we'll just add all bears to both picnics for our many to many relationship
$bearLawly->picnics()->attach($picnicYellowstone->id);
$bearLawly->picnics()->attach($picnicGrandCanyon->id);
$bearCerms->picnics()->attach($picnicYellowstone->id);
$bearCerms->picnics()->attach($picnicGrandCanyon->id);
$bearAdobot->picnics()->attach($picnicYellowstone->id);
$bearAdobot->picnics()->attach($picnicGrandCanyon->id);
$this->command->info('They are terrorizing picnics!');*/
}
}
I have tried running composer dump-autoload but this did not resolve my issue. I am new to Laravel so I do not know too much about it.

I think your model class Bear is not found in BearAppSeeder. So ,
put
use App\Bear;
after
use Illuminate\Database\Seeder;
I hope this may help

Call the seeder class in following way:
$this->call(App\BearAppSeeder::class);
Use App directory name before BearAppSeeder::class

Related

relationship is not working - Laravel / phpunit / Sqlite

I am coding a test, but I get some unexpected results in basic relationships. I use in memory sqlite db.
Those test were working well with MySQL db. I guess it has something to do with this migration.
$tournament0 = factory(Tournament::class)
->create(['user_id' => $this->simpleUser->id]);
$this->simpleUser is created on setUp() method
Now, when $this->simpleUser->tournaments should return $tournament0, but it is empty.
My relationship is working, it is not broken. Also, when testing manually in the browser, it works.
It just fail when using phpunit...
Here is the setUp() Method:
public function setUp()
{
parent::setUp();
$this->simpleUser = factory(User::class)->create(['role_id' => Config::get('constants.ROLE_USER')]);
}
Full method that is failing:
/** #test */
public function dashboard_check_initial_state()
{
Artisan::call('db:seed', ['--class' => 'CountriesSeeder', '--database' => 'sqlite']);
Artisan::call('db:seed', ['--class' => 'TournamentLevelSeeder', '--database' => 'sqlite']);
Artisan::call('db:seed', ['--class' => 'CategorySeeder', '--database' => 'sqlite']);
// Given
$this->logWithUser($this->simpleUser);
// Nothing has been created, default dash
$this->visit('/')
->see(trans('core.create_new_tournament'))
->dontSee(trans('core.congigure_categories'));
// Create 1 tournament
$tournament0 = factory(Tournament::class)->create(['user_id' => $this->simpleUser->id]);
$this->visit('/')
->seeInElement("p.text-muted", trans('core.no_tournament_created_yet'));
// Now configure 2/2 categories
$championship1 = factory(Championship::class)->create(['tournament_id' => $tournament0->id,'category_id'=>1]);
$championship2 = factory(Championship::class)->create(['tournament_id' => $tournament0->id,'category_id'=>2]);
factory(ChampionshipSettings::class)->create(['championship_id' => $championship1->id]);
factory(ChampionshipSettings::class)->create(['championship_id' => $championship2->id]);
$this->visit('/')
->seeInElement("span.text-muted", trans('core.congigure_categories'));
}
Any idea what can it be?

Database Seeder cannot find class with Laravel 5.2

When running php artisan migrate --seed, this error appears:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Class 'CreateCharactersTable' not found.
Here is that that class:
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class CharacterSeeder extends Seeder
{
public function run()
{
DB::table('characters')->delete();
DB::table('characters')->insert([
'user_id' => 999,
'name' => 'Susan Strong',
'race' => 'orc',
'class' => 'assassin',
'image_location' => null,
'combat_level' => '0',
'base_str' => 6,
'base_int' => 4,
'base_apt' => 5,
'mod_str' => 9,
'mod_int' => 5,
'mod_apt' => 7,
'xp_str' => 1,
'xp_int' => 2,
'xp_apt' => 1,
'is_bot' => 1,
'created_at'=> '2017-04-02 17:53:02',
'updated_at'=> '2017-04-02 17:53:02'
]);
DB::table('characters')->insert([
'user_id' => 4,
'name' => 'Chale',
'race' => 'elf',
'class' => 'scholar',
'image_location' => null,
'combat_level' => '0',
'base_str' => 3,
'base_int' => 7,
'base_apt' => 5,
'mod_str' => 6,
'mod_int' => 10,
'mod_apt' => 6,
'xp_str' => 1,
'xp_int' => 2,
'xp_apt' => 1,
'is_bot' => 1,
'created_at'=> '2017-04-02 17:53:02',
'updated_at'=> '2017-04-02 17:53:
}
}
?>
and the seeder:
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use Database\Seeds\CharacterSeeder;
use Database\Seeds\ClassesTableSeeder;
use Database\Seeds\RacesTableSeeder;
use Database\Seeds\UserTableSeeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$this->call(UserTableSeeder::class);
$this->call(CharacterSeeder::class);
$this->call(RacesTableSeeder::class);
$this->call(ClassesTableSeeder::class);
}
}
Running composer dumpautoload passes but does not remove the error. When it was only two seeders, User and Character, it ran well. Despite looking over the new seeders again and again, I cannot determine the error involved.
Any suggestions to get the seeder to run?
Thank you.
If you manually added the seeder files you should first run composer dump-autoload. This will regenerate the autoload_classmap.php file for you, see Composer Dump-Autoload for more info.
You've imported all your seeders from a namespace, but they aren't in a namespace.
use Database\Seeds\CharacterSeeder;
use Database\Seeds\ClassesTableSeeder;
use Database\Seeds\RacesTableSeeder;
use Database\Seeds\UserTableSeeder;
Just remove those lines and you should be good to go.
Class 'CreateCharactersTable' should is Migration. You need check this migration file or class exists.
if you only use seeder . you can exec
php artisan db:seed

No migration could be found with the version number: 1

I tried to followed the codeigniter tutorail on youtube here about creating migration in codeigniter. However, I got error
No migration could be found with the version number: 1
I already set $config['migration_version'] = 1; in Application/Config/migration.php and my migration file for creating users table
Application/migrations/001_Create_User.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_Create_Users extends CI_Migration {
/*
up function is for creating and alert table
*/
public function up()
{
$this->dbforge->add_field(array(
'id' => array(
'type' => 'INT',
'constraint' => 11,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'email' => array(
'type' => 'VARCHAR',
'constraint' => '128',
),
'password' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
));
$this->dbforge->add_key('id',TRUE);
$this->dbforge->create_table('users');
}
/*
down function for rollback table
*/
public function down()
{
$this->dbforge->drop_table('users');
}
}
?>
When I check my database, I saw migration table version is always 0.
Please help me, thanks
In config/migration.php
/*
|--------------------------------------------------------------------------
| Migration Type
|--------------------------------------------------------------------------
|
| Migration file names may be based on a sequential identifier or on
| a timestamp. Options are:
|
| 'sequential' = Default migration naming (001_add_blog.php)
| 'timestamp' = Timestamp migration naming (20121031104401_add_blog.php)
| Use timestamp format YYYYMMDDHHIISS.
|
| If this configuration value is missing the Migration library defaults
| to 'sequential' for backward compatibility.
|
*/
$config['migration_type'] = 'sequential';

laravel returns json string on local machine but integer on elastic beanstalk instance

I am having a strange problem with aws, mysql, laravel and angular.
I have a vagrant instance running locally with both my app and database running on it.
I am using angular on the front-end so when the view is loaded angular makes a request to receive a list of all 'goals' entered by the user. one of the fields in goals is goalStatus. this is either 0 or 1 stored as an integer in the mysql table.
angular checks if this value is 0 or 1 and displays and different table cell depending on the result like so
<th ng-if="goal.goalStatus === '0'"><p class="text-danger">In Progress</p></th>
<th ng-if="goal.goalStatus === '1'"><p class="text-success">Achieved</p></th>
in chrome dev tools when I look at the response result for this request I see goalStatus being returned like so
"goalStatus":"0"
and the angular if statements work as intended.
however when I push this app to a development environment in elastic beanstalk which connects to a mysql rds instance that has the same migrations and seeding run on it dev tools shows goalStatus as this
"goalStatus":0
and the angular if conditions are not met so neither one of the elements displays
So it seems that on the elastic beanstalk instance it is being returned as an integer but on the local machine it is being returned as a string. I dont know weather the problem would be in mysql, laravel, or somewhere else.
any ideas? I have included the laravel migration and seed classes for the table below just in case
migration
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateGoalsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('goals', function(Blueprint $table) {
$table->increments('id');
$table->integer('userId');
$table->string('title');
$table->string('goalDesc');
$table->integer('goalStatus')->nullable();
$table->integer('bodyGoalId')->nullable();
$table->integer('strengthGoalId')->nullable();
$table->integer('distanceGoalId')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('goals');
}
}
seeder
<?php
class GoalsTableSeeder extends Seeder
{
public function run()
{
// Uncomment the below to wipe the table clean before populating
DB::table('goals')->truncate();
$now = date('Y-m-d H:i:s');
$goals = array(array(
'userId' => 1,
'title' => 'first goal title',
'goalDesc' => 'This should describe my goal in text form',
'goalStatus' => 0,
'bodyGoalId' => null,
'strengthGoalId' => null,
'distanceGoalId' => 1,
'created_at' => $now,
'updated_at' => $now),
array(
'userId' => 1,
'title' => 'strength goal title',
'goalDesc' => 'This should describe my strngth goal in text form',
'goalStatus' => 0,
'bodyGoalId' => null,
'strengthGoalId' => 1,
'distanceGoalId' => null,
'created_at' => $now,
'updated_at' => $now),
array(
'userId' => 1,
'title' => 'body goal title',
'goalDesc' => 'This should describe my body goal in text form',
'goalStatus' => 0,
'bodyGoalId' => 1,
'strengthGoalId' => null,
'distanceGoalId' => null,
'created_at' => $now,
'updated_at' => $now)
);
// Uncomment the below to run the seeder
DB::table('goals')->insert($goals);
}
}
So this appears to be an issue with the php mysql driver returning all fields as strings regardless of type. source
My aws elastic beanstalk instance seems to be setup to account for this so it is returning strings and strings and ints as ints whereas my vagrant setup needed to be changed to use php5_mysqlnd driver instead of the one it had and this resolved the issue

Yii relation failing with indexes > 5

I have a very simple relation defined as follows (aId and bId are the primary keys for each table).
class A extends CActiveRecord
{
// #var int32 $aId
}
class B extends CActiveRecord
{
// #var int32 $bId
// #var int32 $aId
public function relations()
{
return array(
'a'=>array(self::HAS_ONE, 'A', 'aId'),
);
}
}
As long as bId <= 5, I can access A through $bModel->a without any problems. What's strange is for bId > 5, $bModel->a is null. I've checked $bModel->aId for bId > 5 and the foreign key is correct. I can even access A with $aModel = A::model()->findByPk($bModel->aId);. I can also manually edit my bIds in the database table, which produces the same result.
I have no idea what's causing the relation to fail for primary key's greater than five. Any suggestions for troubleshooting? I'm at a loss.
EDITED
It turns out I wasn't using the relation properly. I should have used BELONGS_TO.
class B extends CActiveRecord
{
// #var int32 $bId
// #var int32 $aId
public function relations()
{
return array(
'a'=>array(self::HAS_ONE, 'A', 'aId'),
);
}
}
HAS_ONE was causing B to use bId to index A. Since I had five instances of A in my database that worked for bID < 5
Enable query logging in your application config to see what exactly is happening.
Do you get any results when manually running those queries?
'components' => array(
'db' => array(
(..)
'enableParamLogging' => true,
),
'log' => array(
'class' => 'CLogRouter',
'routes' => array(
// Show log messages on web pages
array(
'class' => 'CWebLogRoute',
'categories' => 'system.db.CDbCommand', //queries
'levels' => 'error, warning, trace, info',
//'showInFireBug' => true,
),
(I'd post this as a comment rather than an answer, but it seems I can't)
I recommend you to use this -> Yii Debug Toolbar (it is created by my friend here in Ukraine).
Can you provide mysql structure + some example data. Thanks.

Categories