Created
April 20, 2017 09:18
-
-
Save technoknol/a3b58e35262d1a552fd29b614b383861 to your computer and use it in GitHub Desktop.
Lumen 5.4/Laravel 5.4 Users migration and database Seeder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Migration file | |
// database\migrations\create_users_table.php | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateUsersTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('users', function (Blueprint $table) { | |
$table->increments('id'); | |
$table->string('name'); | |
$table->string('email')->unique(); | |
$table->string('password'); | |
$table->rememberToken(); | |
$table->timestamps(); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('users'); | |
} | |
} | |
// Database seeder file | |
// File : \database\seeds\DatabaseSeeder.php | |
// Run command to seed : php artisan db:seed | |
use Illuminate\Database\Seeder; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Support\Facades\Hash; | |
use App\User; | |
class DatabaseSeeder extends Seeder { | |
/** | |
* Run the database seeds. | |
* | |
* @return void | |
*/ | |
public function run() { | |
Model::unguard(); | |
DB::table('users')->delete(); | |
$users = array( | |
['name' => 'Shyam Makwana', 'email' => '[email protected]', 'password' => Hash::make('secret')], | |
['name' => 'Roshan', 'email' => '[email protected]', 'password' => Hash::make('secret')], | |
); | |
// Loop through each user above and create the record for them in the database | |
foreach ($users as $user) { | |
User::create($user); | |
} | |
Model::reguard(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment