I try to seed a user to the database but cannot. I could easely create the migration tables, so the problem is not in the connection to the database, but I cannot create a user, I get the "Class UsersTableSeeder does not exist" error. How can I solve it? (I use laravel 5.3)
Console Error
Here is my code:
DatabaseSeeder.php
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call(UsersTableSeeder::class);
}
}
UsersTableSeeder.php
<?php
use Illuminate\Database\Seeder;
use App/User;
use Illuminate\Support\Facades\DB;
class UsersTableSeeder extends Seeder
{
public function run()
{
\DB::table('users')->delete();
User::create([
'name' => 'lamin',
'email' => 'lamin#laravel.com',
'password' => bcrypt('lamin')
]);
}
}
Try to run composer dumpauto command and then run seeder again.
Run this command.
composer dump-autoload
It just regenerates the list of all classes that need to be included in the project
Related
using Laravel 7 and Laratrust 6 and need seeding the Table using the seed command but seeding only some tables only. not seeding following tables as well in the database users,role_user,permission_user
my DataBaseSeeder.php
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call(LaratrustSeeder::class);
}
}
how could I fix this problem?
add -> namespace Database\Seeders; in LaratrustSeeder.php in seeder folder
I have a problem running tests in my laravel app.
My app is splitted into separated namespaces. Laravel App namespace is in app directory and it's App/ namespace. I have additional namespace in src directory.
My TestCase look like that:
<?php
namespace Tests\Unit;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use PHPUnit\Framework\TestCase;
use SmoothCode\Sample\Domain\User\User;
use SmoothCode\Sample\Domain\User\UserRepository;
use SmoothCode\Sample\Domain\User\ValueObject\ConfirmationCode;
use SmoothCode\Sample\Shared\ValueObjects\Email;
use SmoothCode\Sample\Shared\ValueObjects\Id;
use SmoothCode\Sample\Shared\ValueObjects\Password;
use Tests\CreatesApplication;
class UserDomainTest extends TestCase
{
use CreatesApplication;
protected UserRepository $userRepository;
public function testUserCreation() {
$user = User::create(
Id::generate(),
'Jan',
'Kowalski',
new Email('test#test.com'),
'123123123',
new Password('Pass123!'),
new \DateTimeImmutable(),
ConfirmationCode::generate()
);
//
// $this->assertInstanceOf(User::class, $user);
}
protected function setUp(): void
{
parent::setUp();
}
}
After running vendor/bin/phpunit I'm getting following error:
1) Tests\Unit\UserDomainTest::testUserCreation
RuntimeException: A facade root has not been set.
/home/jakub/Development/Projects/streetboss-server/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:258
/home/jakub/Development/Projects/streetboss-server/src/Sample/Shared/ValueObjects/Password.php:15
/home/jakub/Development/Projects/streetboss-server/tests/Unit/UserDomainTest.php:29
From that i know that the problem lies in src/Sample/Shared/ValueObjects/Password.php:15
which looks like:
<?php
namespace SmoothCode\Sample\Shared\ValueObjects;
use Illuminate\Support\Facades\Hash;
use Webmozart\Assert\Assert;
class Password {
protected string $hash;
public function __construct($plainPassword)
{
Assert::minLength($plainPassword, 6);
$this->hash = Hash::make($plainPassword);
}
public function hashedPassword()
{
return $this->hash;
}
}
I was trying to run:
php artisan config:cache
php artisan cache:clear
php artisan config:clear
composer dump-autoload
But I'm still getting this error.
Okay, I have found a solution for this error. For anyone who would have same problem:
My UserDomainTest was extending TestCase from namespace:
use PHPUnit\Framework\TestCase;
when I've changed to:
use Illuminate\Foundation\Testing\TestCase;
everything works like a charm.
The Jakub's answer don't work for me, so I will explain what I do.
I have the file App/BusinessRules/Admin/Tests/EnvTest.php.
Even using anyone of this 2 namespaces the fail occurs.
So in my EnvTest I extend the tests/TestCase.php file from the namespace:
use Tests\TestsCase
I have seeder classes that I confirmed they could run individually, and I'm trying to run them from another seeder class in the same namespace.
I'm getting an error saying that the class run from another seeder is not found.
I'm using OctboerCMS build 419.
Seeder Class calling subseeder
<?php namespace Cocci\Custom\Updates;
use Seeder;
class SeedTablesCocciPedale extends Seeder
{
public function run()
{
$this->call('Cocci\Custom\Updates\SeedTablesCocciBike005');
$this->call('Cocci\Custom\Updates\SeedTablesCocciColor');
// $this->call(SeedTablesCocciBike005::class);
// $this->call(SeedTablesCocciColors::class);
}
}
Seeder called by another seeder
<?php namespace Cocci\Custom\Updates;
use Seeder;
use Cocci\Custom\Models\Product;
use Cocci\Custom\Models\Part;
use Cocci\Custom\Models\PreviewType;
use Cocci\Custom\Models\PartType;
use Cocci\Custom\Models\PartPreviewPosition;
class SeedTablesCocciBike005 extends Seeder
{
public function run()
{
$partTypeSetAll = PartType::create([
'name' => 'set_all',
'category' => PartType::CAT_ALL_PARTS,
]);
$partTypeNoPaintParts = PartType::create([
'name' => 'no_paint_parts',
'category' => PartType::CAT_NON_CUSTOMIZABLE,
]);
...
}
}
Error
% php artisan plugin:refresh Cocci.Custom
Rolled back: Cocci.Custom
Reinstalling plugin...
PHP Fatal error: Class 'Cocci\Custom\Updates\SeedTablesCocciBike005' not found in /Library/WebServer/Documents/cocci-custom/vendor/laravel/framework/src/Illuminate/Database/Seeder.php on line 62
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'Cocci\Custom\Updates\SeedTablesCocciBike005' not found
I tried both ways specifying the class by string of fully qualified name and with class keyword, but neither worked. Probably these seeder classes are not loaded?
What should I do?
Thanks in advance.
When using a seeder class to call additional seeder classes, you need to make sure you're autoloading the seeder classes.
Assuming you're seeder classes are called seed_tables_cocci_bike_005.php and seed_tables_cocci_color.php, you can add these files to you're classmap dev autoloader as follows.
"autoload-dev": {
"classmap": [
"tests/TestCase.php",
"tests/UiTestCase.php",
"tests/PluginTestCase.php",
"plugins/cocci/custom/updates/seed_tables_cocci_bike_005.php",
"plugins/cocci/custom/updates/seed_tables_cocci_color.php"
]
}
Remember to composer dumpautoload once your composer.json file has been updated.
I am using Laravel on centos 7,I have some routes that are used to initiate some data,like this:
Route::get('init-users', 'InitController#initUsers');
Route::get('init-roles', 'InitController#initRoles');
//...
//...
//...
I want to write a shell script file to run the routes above,what command should I use to do it?
While you could use curl to accomplish this, Laravel actually has built in functionality for this, called Seeding.
Essentially, you'd do something like this:
php artisan make:seeder UsersTableSeeder
Then edit your UsersTableSeeder file:
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
DB::table('users')->insert([
'name' => str_random(10),
'email' => str_random(10).'#gmail.com',
'password' => bcrypt('secret'),
]);
}
}
Then finally: php artisan db:seed
Follow the link above for more information about it, as I gave you a very basic example.
I'm using Lumen and working through a 12 month-old tutorial.
While attempting to run php artisan db:seed, I'm getting the following error:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Class 'App\Models\Quote' not found
However I created the Models sub-directory and Quote.php within that directory. Here is that code:
<?php
# app/Models/Quote.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
final class Quote extends Model
{
}
Within database/seeds I have two files: QuoteTableSeeder.php & the default DatabaseSeeder.php. I added $this->call('QuoteTableSeeder'); to the run() method of DatabaseSeeder.php.
Here are the contents of QuoteTableSeeder.php:
<?php
# database/seeds/QuoteTableSeeder.php
use App\Models\Quote;
use Illuminate\Database\Seeder;
class QuoteTableSeeder extends Seeder
{
public function run()
{
Quote::create([
'text' => 'Success is going from failure to failure without losing your enthusiasm',
'author' => 'Winston Churchill',
'background' => '1.jpg'
]);
}
}
I have already ran composer dump-autoload which fixed a previous error but now causes the above-mentioned issue.
What am I doing wrong?