Laravel Forge database seeder - laravel

I'm trying to insert a user into my laravel forge site automatically, but the seeder is not working. I can create a user and log in, but I'm trying to have a default admin user once the site is live.
class UsersTableSeeder extends Seeder
{
public function run()
{
DB::table('users')->insert([
'name' => 'Jovani Calixte',
'is_admin' => 1,
'email' => 'user1#email.com',
'password' => bcrypt('password'),
]);
}
}
and this the database user php file
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call(UsersTableSeeder::class);
}
}
When I deploy the site to forge and try to log in I get the following error. What am I doing wrong?
Whoops! Something went wrong.
These credentials do not match our
records.

If you want to seed the database on the Forge provisioned server, you can log into Forge and navigate to your server/site, and click "Commands" in the navbar on the left, you can then run the command for your seeder,
php artisan db:seed
or
php artisan db:seed --class=UsersTableSeeder
To ensure that this data did indeed seed, you should check your users table to make sure that the record is in the table.

Related

Database Factory records don't get pushed to the DB in Laravel

I have created Factory and Seeder for my Posts in Laravel. The code works fine in Tinker as it creates records successfully. Also when I migrate:fresh --seed also displays successful action. However, it doesn't push the records into the db.
After many attempts I have managed to seed fake records into the db. When I tried to repeat the process to check if it is working every time, it failed again.
What am I missing? I reboot the server every time but it doesn't seem to give the results as desired.
I ma using Laravel Homestead with vagrant VM.
This is my PostTableSeed
class PostsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Post::factory(41)->create();
}
}
This is my PostFactory
public function definition()
{
return [
'title' => $this->faker->unique()->sentence(),
'content' => $this->faker->realText($maxNbChars = 550),
'image' => $this->faker->imageUrl(1080, 985),
'type' => $this->faker->sentence(),
];
}
}

How can I insert the super_admin seeder in laravel?

SuperAdminSeeder.php
public function run()
{
#Super Admin Seeder
$role = Role::where('name', 'super_admin')->first();
$user = User::create([
'name' => 'Jhon Cena',
'email' => 'jhon#gmail.com',
'password' => Hash::make('jhoncena'),
'remember_token' => Str::random(60),
]);
$user->role()->attach($role->id);
}
This is my SuperAdmin seeder which task is to make a user with role super_admin
DatabaseSeeder.php
public function run()
{
$this->call(RegionSeeder::class);
$this->call(CountrySeeder::class);
$this->call(LocationSeeder::class);
$this->call(ProductSeeder::class);
$this->call(SliderSeeder::class);
$this->call(BannerSeeder::class);
$this->call(SuperAdminSeeder::class); //this is the Super Admin seeder
$this->call(RoleSeeder::class);
$this->call(UserSeeder::class);
$this->call(ShopSeeder::class);
}
This is the database seeder
The error I am getting [php artisan migrate:fresh --seed]
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Trying to get property 'id' of non-object", "E:\github\LARAVEL\Deal-Ocean\database\seeds\SuperAdminSeeder.php", [Object(App\User)])
I am new in laravel
It's caused no super_admin role when you run superadmin seeder. You should add SuperAdminSeeder after RoleSeeder.
public function run()
{
$this->call(RegionSeeder::class);
$this->call(CountrySeeder::class);
$this->call(LocationSeeder::class);
$this->call(ProductSeeder::class);
$this->call(SliderSeeder::class);
$this->call(BannerSeeder::class);
$this->call(RoleSeeder::class); //this seeder must have superadmin role
$this->call(SuperAdminSeeder::class); //this is the Super Admin seeder
$this->call(UserSeeder::class);
$this->call(ShopSeeder::class);
}
public function run()
{
$this->call(RegionSeeder::class);
$this->call(CountrySeeder::class);
$this->call(LocationSeeder::class);
$this->call(ProductSeeder::class);
$this->call(SliderSeeder::class);
$this->call(BannerSeeder::class);
$this->call(RoleSeeder::class);
$this->call(SuperAdminSeeder::class); //this is the Super Admin seeder
$this->call(UserSeeder::class);
$this->call(ShopSeeder::class);
}
Just put the RoleSeeder above the SuperAdminSeeder.
Because roles table might have a column value named super_admin. SO you must call the RoleSeeder Because there is a foreign key constriant.. And then you can insert the super admin using SuperAdminSeeder

Getting class does not exist error when running database seeder

I am creating a seeder in laravel 6.1 but I keep getting this error
Illuminate\Contracts\Container\BindingResolutionException : Target class [AdminsTableSeeder] does not exist.
I tried running composer dump-autoload and composer dumpautoload, it doesn't work for me.
here is my AdminsTableSeeder.php
use App\Models\Admin;
use Faker\Factory as Faker;
use Illuminate\Database\Seeder;
class AdminsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$faker = Faker::create();
Admin::create([
'name' => $faker->name,
'email' => 'admin#admin.com',
'password' => bcrypt('password'),
]);
}
}
and here is my DatabaseSeeder.php
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run()
{
$this->call(AdminsTableSeeder::class);
}
}
Make sure your AdminsTableSeeder.php file is in the same directory where you have your DatabaseSeeder.php file.
Run
composer dump-autoload
then try
php artisan db:seed
Optional.
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run(){
$this->call('AdminsTableSeeder');
}
}
try with $this->call('AdminsTableSeeder'); like this.
In your case, move all seeder files from previous database/seeds directory to database/seeders folder & then run composer dump-autoload.
Remember, from laravel 8 seeders and factories are namespaced
To accommodate for these changes,
[1] - Add Database\Seeders namespace to your seeder classes.
namespace Database\Seeders;
[2] - Move all seeder files to database/seeders folder.
[3] - If you import any seeders classes in DatabaseSeeder file then remove all of them. (simply remove all lines that started with use Database\Seeders\... from DatabaseSeeder.php)
[4] - Finally run dump-autoload.
composer dump-autoload
You can now try a fresh migration with seed,
php artisan migrate:fresh --seed
For my case(I use Laravel 8), I solved my problem by modifying the RouteServiceProvider.php file in App/Providers/ path. I uncommented code on line 29.
protected $namespace = 'App\\Http\\Controllers';
It worked for me.
run
composer dump-autoload
then try
php artisan db:seed
For Laravel 8:
I have the same issue and I found a solution in Laravel doc and it's worked for me.
https://laravel.com/docs/8.x/upgrade#seeder-factory-namespaces
Update Composer:
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
}
Run:
composer dumpautoload
php artisan db:seed --force
Concerning my case, I used the latest Laravel 8 which is the latest version, I solved my problem by changing the RouteServiceProvider.php file in App/Providers/ path by uncommenting the code on line 29.
protected $namespace = 'App\Http\Controllers';
For Laravel ^7.0
If your using Laravel Eloquent
Example:
<?php
use App\Models\User;
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
public function run()
{
$users = [
[
'id' => 1,
'name' => 'Admin',
'email' => 'admin#admin.com',
'password' => bcrypt('password'),
'remember_token' => null,
],
];
User::insert($users);
}
}
If your using Laravel Query Builder
Example:
<?php
//Do not use -> namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class UsersTableSeeder extends Seeder
{
public function run()
{
DB::table('users')->insert([
'name' => 'Admin',
'email' => 'admin#admin.com',
'password' => bcrypt('password'),
'remember_token' => null,
]);
}
}
In your DatabaseSeeder.php
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call([
UsersTableSeeder::class,
]);
}
}
Seems like the controller name is case-sensitive in Laravel 8. So my suggestion is to double-check the controller name.
For instance:
in web.php avoid calling
UserAPIController
as
UserApiController
(API as api)
It may fix this error.
In your DatabaseSeeder.php, you can add the nameSpace for AdminsTableSeeder like -
use App\Models\Admin\AdminsTableSeeder;
Closing the current running serve before doing db:seeds

laravel how to send email on new user registration only to admin email

I need to send a notification email only to admin when new user is registered.
When i submitting registration getting error -
Class 'app\Mail\NewUser' not found
in RegisterController.php (line 88)
Here is controller:
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use app\Mail\NewUser;
use Illuminate\Support\Facades\Mail;
protected function create(array $data)
{
$user = User::create([
'companyname' => $data['companyname'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'VAT' => $data['VAT'],
'companyphone' => $data['companyphone'],
'companystreet' => $data['companystreet'],
'companycity' => $data['companycity'],
'companycountry' => $data['companycountry'],
'companypostcode' => $data['companypostcode']
]);
Mail::to('example#gmail.com')->send(new NewUser());
return $user;
}
would be great if someone help me.
Because you may not have created NewUser mailable class. You can create mailable classes by following artisan command.
php artisan make:mail NewUser
To know how to configure mali parameters like sender, from, to, subject, body etc., refer Writing Mailable here
Source
Update:
If you have created mailable and not able to find it, most probably it's composer issue. Fire following commands to clear cache and autoload files.
composer dump-autoload
php artisan config:cache

'php artisan db:seed' doesn't work in Laravel5.4

I have started learning laravel 5.4. In order to populate my db, I complete BreedsTableSeeder.php file.
enter code here
class BreedsTableSeeder extends Seeder {
public function run() {
DB::table('breeds')->insert([
['id' => 1, 'name' => "Domestic"],
['id' => 2, 'name' => "Persian"],
['id' => 3, 'name' => "Siamese"],
['id' => 4, 'name' => "Abyssinian"],
]);
}
}
Then I complete DatabaseSeeder.php.
enter code here
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder {
public function run() {
$this->call(BreedsTableSeeder::class);
}
}
?>
Then, I seed the database by calling it, using the following command.
$ php artisan db:seed
but error occurs as follows
*
[ReflectionException]
Class BreedsTableSeeder does not exist*
When I use,
php artisan db:seed --class="BreedsTableSeeder"
the result is the same.
When I use,
use Illuminate\Database\Eloquent\Model; on DatabaseSeeder.php file
it is automatically deleted.
You need to do
composer dumpautoload
once you create new classes
EDIT
error occurs like this. "Class 'Seeder' not found
Your seeder lacks
use Illuminate\Database\Seeder;
I faced this problem. Now i solved.
And you need to run in your console:
composer dump-autoload
to generate new class map and then run:
php artisan db:seed
I've just tested it. It is working without a problem in Laravel 5

Resources