2015-12-20 45 views
7

私はLaravel(4.2)を使用していますLaravel:データベースに最初のユーザーを挿入するには

私は認証システムを使ってプロジェクトを進めています。最初のユーザーを自分のユーザーテーブルに挿入する必要があります。私はSQLコマンド(ユーザーに挿入....)で直接それをしたいと思います。この最初のユーザーは、伝統的なラベリング方法では作成できないためです。

この最初のユーザは、テーブルに挿入された後、auth :: attemptsメソッドで識別されます。

このユーザーをどのようにしてmysqlテーブルに挿入しますか?

のようなものがありますか?

insert into users (login, password) values ('admin', 'crypted password which can be later identified with laravel') 
+0

'Laravel'から' SQL'を実行しますか?もしあなたがなぜ 'phpMyAdmin'か' MySql-console'からその 'SQL'を実行していないのであれば。 –

+0

Laravelはルート、コントローラ、モデルと連携して動作します。データベースクエリを実行する前に、すでにそれらを設定しているとします。 – Franco

+0

Franco:いいえこれらの要件はまだありません。他のユーザーのために私はもちろん持っています。しかし、最初の1つは、どのようにデータベースにそれを作成するには? – Dom

答えて

1

もっとも合理的な方法は、このような操作にDatabaseSeederを使用することです。

私は、ドキュメントから直接例を使用します:私はDatabase seeding in Laravel

11

php artisan migrate後にあなたがphp artisan db:seedを実行することができますし、あなたの第一ユーザーが

参考に作成され、そのようなシーダを作成した後

class DatabaseSeeder extends Seeder { 

    public function run() 
    { 
     $this->call('UserTableSeeder'); 

     $this->command->info('User table seeded!'); 
    } 

} 

class UserTableSeeder extends Seeder { 

    public function run() 
    { 
     DB::table('users')->delete(); 

     User::create(array('login' => 'admin', 'password' => Hash::make('secret'))); 
    } 
} 

職人と一緒にシーダーを作成する:

php artisan make:seeder UsersTableSeeder 

、あなたがファイルを開いて、ユーザーを入力します。

use Illuminate\Database\Seeder; 

class UsersTableSeeder extends Seeder 
{ 
    /** 
    * Run the database seeds. 
    * 
    * @return void 
    */ 
    public function run() 
    { 
     DB::table('users')->insert([ 
      'name' => 'User1', 
      'email' => '[email protected]', 
      'password' => bcrypt('password'), 
     ]); 
     DB::table('users')->insert([ 
      'name' => 'user2', 
      'email' => '[email protected]', 
      'password' => bcrypt('password'), 
     ]); 
    } 
} 

あなたがユーザーのランダムなリストを生成したい場合は、ファクトリを使用することができます

use Illuminate\Database\Seeder; 

class UsersTableSeeder extends Seeder 
{ 
    /** 
    * Run the database seeds. 
    * 
    * @return void 
    */ 
    public function run() 
    { 
     factory(App\User::class, 50)->create(); 
     /* or you can add also another table that is dependent on user_id:*/ 
     /*factory(App\User::class, 50)->create()->each(function($u) { 
      $userId = $u->id; 
      DB::table('posts')->insert([ 
       'body' => str_random(100), 
       'user_id' => $userId, 
      ]); 
     });*/ 
    } 
} 

次に、ファイルにapp/database/seeds/DatabaseSeeder.php実行機能のコメントを解除または追加するには、次の行を追加してください:

$this->call(UsersTableSeeder::class); 

それは次のようになります。私は誰かを助けることを願っています

php artisan db:seed 

または

php artisan db:seed --class=UsersTableSeeder 

:実行終了時

use Illuminate\Database\Seeder; 

class DatabaseSeeder extends Seeder 
{ 
    /** 
    * Run the database seeds. 
    * 
    * @return void 
    */ 
    public function run() 
    { 
     $this->call(UsersTableSeeder::class); 
    } 
} 

。 PS:これは私たちがデータに直接新しいユーザーを追加することができますPHPの職人いじくりツールを使用して、

http://connectonline.in/add-new-user-to-laravel-database/

は、URLの上ご確認くださいウェブサイト内の溶液を探す

+1

5.4、おかげで素晴らしい作品! – AJMaxwell

1

Laravel 5.3で行われましたベース。あなたは私はあなたのためにユーザを作成するコマンドを作成することをお勧めリポジトリまたはサーバーにテキスト形式で安全でないユーザーのパスワードの保存を懸念している場合は

$user = new App\User(); 
$user->password = Hash::make('password here '); 
$user->email = 'proposed [email protected]'; 
$user->save(); 
2

参照Laravelのドキュメント:ハンドル法でhttps://laravel.com/docs/5.6/artisan#generating-commands

このような何かを追加します。

public function handle() 
{ 
    $first_name = $this->ask('What is the first name?'); 
    $last_name = $this->ask('What is the last name?'); 
    $email = $this->ask('What is the email address?'); 
    $password = $this->secret('What is the password?'); 

    AdminUser::create([ 
     'first_name' => $first_name, 
     'last_name' => $last_name, 
     'email' => $email, 
     'password' => bcrypt($password) 
    ]); 

    $this->info("User $first_name $last_name was created"); 
} 

あなたは、サーバからのコマンドを実行することができ、あなたがテキストに何も保存されていませんベースのフォーマット!

関連する問題