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
Related
I want to write a correct PostFactory for seed my DB with Laravel8
I follow the documentation on Laravel8 for make my Factory for seed my database
https://laravel.com/docs/8.x/seeding#using-model-factories
I have 3 Models :
Category.php
Post.php
User.php
I can seed my DB when i use this command :
php artisan db:seed --class=UserSeeder
php artisan db:seed --class=CategorySeeder
But i can't seed :
php artisan db:seed --class=PostSeeder
php artisan db:seed for seed all DB with one command
My PostSeeder :
class PostSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
\App\Models\Post::factory(50)->create();
}
}
My PostFactory :
public function definition()
{
return [
'title' => $this->faker->sentence(rand(5, 10)),
'content' => $this->faker->sentences(50, true),
'image' => 'https://via.placeholder.com/350/65' . rand(1, 100),
'created_at' => now(),
'updated_at' => now(),
'category_id' => Category::inRandomOrder()->first()->id,
'users_id' => User::inRandomOrder()->first()->id,
];
}
My PostFactory does'nt want to take my seed
I encounter this error :
PS C:\Users\Chris\Desktop\Laravel_Projects\Blog> php artisan db:seed
ErrorException
Trying to get property 'id' of non-object
at C:\Users\Chris\Desktop\Laravel_Projects\Blog\database\factories\
28▕ 'content' => $this->faker->sentences(50, true),
29▕ 'image' => 'https://via.placeholder.com/350/65'
30▕ 'created_at' => now(),
31▕ 'updated_at' => now(),
1 C:\Users\Chris\Desktop\Laravel_Projects\Blog\database\factories\PostFactory.php:32
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Trying to get property 'id' of non-object", "C:\Users\Chris\Desktop\Laravel_Projects\Blog\database\factories\PostFactory.php", [])
2 C:\Users\Chris\Desktop\Laravel_Projects\Blog\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Factories\Factory.php:424
Database\Factories\PostFactory::definition()
My problem come with my 2 foreign keys :
I see the other post with this error but i can't debug with a dd();
I don't found how write my definition for my 2 foreign keys
I have try to check() my id but it'same result.
And to seed my posts_table after and before my users_table and catergories_table it's same.
I tried things with the different posts on the subject without success..Any help is appreciated.
Use the random id it is create random id and how many create record so you define in your seeder.this two line add on your post factory. i hope help you.
'category_id' => Category::all()->random()->id,
'users_id' => User::all()->random()->id,
I am working on a Social Network application with Codeigniter 3, Ion-Auth and Bootstrap 4. You can see the Github repo HERE.
I have enabled migrations, then created the migration file 002_add_messages_table.php:
class Migration_Create_Messages_Table extends CI_Migration {
public function up()
{
$this->dbforge->add_field(array(
'id'=>array(
'type'=>'INT',
'constraint' => 11,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'from'=>array(
'type'=>'INT',
'constraint' => 11,
'null' => FALSE
),
'to'=>array(
'type'=>'INT',
'constraint' => 11,
'null' => FALSE
),
'message'=>array(
'type'=>'TEXT',
),
'status'=>array(
'type'=>'TINYINT',
'constraint' => 1,
),
'created_at'=>array(
'type'=>'TIMESTAMP',
'default' => NULL
),
));
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('messages');
}
public function down() {
$this->dbforge->drop_table('messages');
}
}
I also have a Migrate.php controller with this content:
<?php defined("BASEPATH") or exit("No direct script access allowed");
class Migrate extends CI_Controller{
public function index($version){
$this->load->library("migration");
if(!$this->migration->version($version)){
show_error($this->migration->error_string());
} else {
echo "Tables created";
}
}
}
When I access http://myapp.com/index.php/migrate/index/002, instead of creating a messages table, the application throws the message:
No migration could be found with the version number: 002.
What am I doing wrong?
*** UPDATED ***
First off, edit application/config/migration.php and set
$config['migration_type'] = 'sequential'; // was 'timestamp' in your repo
Secondly, edit your migration file and either change class name to
class Migration_Add_Messages_Table extends CI_Migration {
or rename the file as 002_create_messages_table
(class name and file name need to be similar)
THEN
If the following query
SELECT version FROM migrations
is returning anything greater than or equal to 2, what you can do is decrease the version value in your migrations table, and re-run the migration (if it's not on automatic).
I have a project made with slim, Eloquent and Phinx and I am integrating PHPunit.
Everything works correctly except the new test that I need to perform an information seeder before executing the test.
The seed
<?php
use Phinx\Seed\AbstractSeed;
class Permissions extends AbstractSeed
{
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeders is available here:
* http://docs.phinx.org/en/latest/seeding.html
*/
public function run()
{
$data = [
[
'id' => '1',
'level' => 'admin',
'created_at' => date('Y-m-d H:i:s'),
],
];
$level = $this->table('users_levels');
$level->insert($data)
->save();
$data = [
[
'users_level_id' => '1',
'method' => 'GET',
'url' => '/api/events/{date:\d{4}-\d{1,2}-\d{1,2}}',
'created_at' => date('Y-m-d H:i:s'),
],
[
'users_level_id' => '1',
'method' => 'POST',
'url' => '/api/event',
'created_at' => date('Y-m-d H:i:s'),
],
];
$urls = $this->table('level_urls');
$urls->insert($data)
->save();
}
}
I run it from the console works perfectly,
C:\xampp\htdocs\CirceApi> .\vendor\bin\phinx seed:run
Phinx by CakePHP - https://phinx.org. 0.8.1
using config file .\phinx.php
using config parser php
using migration paths
- C:\xampp\htdocs\CirceApi\database\migrations
using seed paths
- C:\xampp\htdocs\CirceApi\database\seeds
warning no environment specified, defaulting to: development
using database circe
== Permissions: seeding
== Permissions: seeded 0.1773s
but when I launch it in the following way for the tests
use Phinx\Console\PhinxApplication;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\NullOutput;
protected function runMigration()
{
$app = new PhinxApplication();
$app->setAutoExit(false);
$app->doRun(new StringInput("migrate"), new NullOutput());
$app->doRun(new StringInput("seed:run"), new NullOutput());
}
it returns the following error.
14) Tests\Profile\ProfileTest::it_returns_404_status_code_when_profile_is_not_found
PDOException: SQLSTATE[HY000]: General error: 1 table users_levels has no column named level
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Db\Adapter\PdoAdapter.php:215
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Db\Adapter\AdapterWrapper.php:191
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Db\Adapter\TimedOutputAdapter.php:125
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Db\Table.php:667
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Db\Table.php:610
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Db\Table.php:697
C:\xampp\htdocs\CirceApi\database\seeds\Permissions.php:29
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Migration\Manager\Environment.php:156
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Migration\Manager.php:403
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Migration\Manager.php:536
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Console\Command\SeedRun.php:110
C:\xampp\htdocs\CirceApi\vendor\symfony\console\Command\Command.php:255
C:\xampp\htdocs\CirceApi\vendor\symfony\console\Application.php:901
C:\xampp\htdocs\CirceApi\vendor\symfony\console\Application.php:262
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Console\PhinxApplication.php:83
C:\xampp\htdocs\CirceApi\tests\UseDatabaseTrait.php:19
C:\xampp\htdocs\CirceApi\tests\BaseTestCase.php:44
ERRORS!
Tests: 17, Assertions: 7, Errors: 14.
Thanks for the help.
Try this:
use Phinx\Console\Command\SeedRun;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
$phinxApplication = new Application();
$phinxApplication->add(new SeedRun());
$phinxSeedRunCommand = $phinxApplication->find('seed:run');
$phinxCommandTester = new CommandTester($phinxSeedRunCommand);
$phinxCommandTester->execute(['command' => $phinxSeedRunCommand->getName()]);
$phinxDisplay = $phinxCommandTester->getDisplay();
$phinxStatusCode = $phinxCommandTester->getStatusCode();
if ($phinxStatusCode > 0) {
throw new RuntimeException('Seed:run failed');
}
I've moved my code from Laravel 5.2 to 5.3, everything is working fine except when I run my DatabaseSeeder. This was working perfectly in Laravel 5.2, now when I try to run php artisan db:seed in 5.3 I get this error when the Seeder uses Faker:
[BadMethodCallException]
Call to undefined method Illuminate\Database\Query\Builder::lists()
Everything seems to be installed correctly.
Code example:
<?php
use App\Models\Gallery;
use App\Models\GalleryImage;
use Illuminate\Database\Seeder;
class GalleryImageTableSeeder extends Seeder
{
public function run()
{
DB::table('gallery_images')->delete();
$faker = Faker\Factory::create();
$gallery = Gallery::lists('id')->All();
foreach(range(1, 98) as $index) {
GalleryImage::create([
'page_id' => null,
'gallery_id' => $faker->randomElement($gallery),
'alt' => 'Image description',
'large' => '201508183828fh5ntu80ub-or.jpg',
'medium' => '201508183828fh5ntu80ub-lg.jpg',
'thumbnail' => '201508183828fh5ntu80ub-th.jpg',
'order' => 1,
'cover' => false,
'visibility' => 'visible',
]);
}
}
}
At last i found it out mysql. Need to change list to pluck
Example:
$gallery = Gallery::pluck('id')->All();
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