Laravel Tutorials
- What is laravel
- Laravel Installation
- Directory Structure
- htaccess
- Remove public from url
- Artisan Command
- Laravel Configuration
- Routing Configuration
- Namespaces
- Request
- Response
- Controller
- Model
- User Authentication
- Multi User Authentication
- Database Seeding
- Database
- Database Query
- ORM
- One-to-One Relationship
- One-to-Many Relationship
- Many to Many Eloquent Relationship
- Has One Through
- Has Many Through
- Querying Relations
- Middleware
- Laravel Views
- Blade Views
- Print data on view page
- Get Site URL
- Get URL Segment
- Get images from Storage folder
- Clear cache
- Form Class not found
- Flash Message in laravel
- Redirections
- path
- CRUD Projerct
- CRUD in Laravel
- CRUD progran
- Laravel Validation
- Jquery Validation
- Cookie
- Session
- Email Send in laravel
- File uploading
- CSRF Protection
- Helper in Laravel
- Helper Functions
- Guzzle Http Client
- Paypal Payment Gatway Integration
- Cron Job in laravel
- Flash message
- path
- Errors Handling
- Date Format
- Date Format Validation
- Display Image on View Page
- Update User Status using toggle button
- Delete Multiple Records using Checkbox in Laravel?
- Confirmation Before Delete Record
- Delete image from storage
- Remove/Trim Empty & Whitespace From Input Requests
- Block IP Addresses from Accessing Website
- How to Disable New User Registration in Laravel
- Redirect HTTP To HTTPS Using Laravel Middleware
- CKEditor
- slug generate unique
- Prevent Browser's Back Button After Logout
- Datatable dunamically
- encrypt & Decript
- Download File
- Rest API
- Shopping Cart
- Shopping Cart Example
- Dynamic Category-Subcategory Menu
- Ajax Search
- Interview Question
- laravel Tutorilal link
- laravel Tutorilal
Important Links
Database: Seeding
Laravel includes a simple method of seeding your database with test data using seed classes. All seed classes are stored in the database/seeds directory. Seed classes may have any name you wish.
By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes
Create Seeders
To generate a seeder, execute the make:seeder Artisan command. All seeders generated by the framework will be placed in the database/seeds directory
php artisan make:seeder UserSeeder
php artisan make:seeder UsersTableSeeder
// here UsersTableSeeder is the class name of seeder class.
After that seeder class create UserSeeder class in database/seeds directory.
<?php
use Illuminate\Database\Seeder;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
}
}
insert table data in run method.
A seeder class only contains one method by default: run. This method is called when the db:seed Artisan command is executed. Within the run method, you may insert data into your database however you wish. You may use the query builder to manually insert data or you may use Eloquent model factories.
<?php
use Illuminate\Database\Seeder;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('users')->insert([
'name' => 'mahtab',
'email' => 'mahtab@email.com',
'password' => bcrypt('mahtab'),
//'password' => Hash::make('mahtab'),
//'remember_token' => str_random(10),
]);
DB::table('users')->insert([
'name' => 'nusrat',
'email' => 'nusrat@email.com',
'password' => bcrypt('nusrat'),
//'password' => Hash::make('nusrat'),
//'remember_token' => str_random(10),
]);
}
public function run()
{
User::create([
'name' => 'John Smith',
'email' => 'john_smith@gmail.com',
'password' => Hash::make('password'),
'remember_token' => str_random(10),
]);
}
}
public function run()
{
DB::table('users')->insert([
'name' => Str::random(10),
'email' => Str::random(10).'@gmail.com',
'password' => Hash::make('password'),
]);
}
public function run()
{
for ($i=0; $i < 3; $i++) {
User::create([
'name' => str_random(8),
'email' => str_random(12).'@mail.com',
'password' => bcrypt('123456')
]);
}
}
DatabaseSeeder class
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call(UserSeeder::class);
$this->command->info('User table seeded!');
//$this->call(CountriesTableSeeder::class);
}
}
Running Seeders
Once you have written your seeder, you may need to regenerate Composer's autoloader using the dump-autoload command
composer dump-autoload
Now you may use the db:seed Artisan command to seed your database. By default, the db:seed command runs the DatabaseSeeder class, which may be used to call other seed classes. However, you may use the --class option to specify a specific seeder class to run individually:
php artisan db:seed
Seeding: UsersTableSeeder
Seeded: UsersTableSeeder (0.29 seconds)
User table seeded!
Database seeding completed successfully.
Or
php artisan db:seed --class=UserSeeder
You may also seed your database using the migrate:fresh command, which will drop all tables and re-run all of your migrations. This command is useful for completely re-building your database
php artisan migrate:fresh --seed
Forcing Seeders To Run In Production
Some seeding operations may cause you to alter or lose data. In order to protect you from running seeding commands against your production database, you will be prompted for confirmation before the seeders are executed. To force the seeders to run without a prompt, use the --force flag
php artisan db:seed --force
<