DB設定
$ mysql -u <UserName> -p
- 'Enter password:'と聞かれるのでパスワードを入力する
Enter password: <Password>
mysql> quit;
ユーザ
mysql> select User,Host from mysql.user;
+---------+-----------+
| User | Host |
+---------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
+---------+-----------+
3 rows in set (0.00 sec)
- ユーザ作成
ホスト名を省略すると'<ユーザ名>'@''%(ワイルドカード)'と記述したものとして扱われる
mysql> create user '<UserName>'@'<HostName>' identified by '<Password>';
- ユーザの削除
間違えたコマンドで不要なユーザが大量生産されたので削除しましたw
mysql> drop user '<UserName>'@'<HostName>';
GRANT <操作名> ON <データベース名>.<テーブル名>TO <ユーザ名>@<ホスト名> IDENTIFIED BY '<パスワード>';
データベース
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
mysql> create database <DatabaseName>;
mysql> use <DatabaseName>;
テーブル
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
=============================
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
28 rows in set (0.00 sec)
$ php artisan make:migration <マイグレーションファイル名> --create=<作成したいテーブル名>
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('<作成したいテーブル名>', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::drop('<作成したいテーブル名>');
}
}
$ php artisan migrate
シーダー
db:seed
コマンドで実行されるdatabase/seeds/DatabaseSeeder.phpを編集
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call(UsersTableSeeder::class);
}
}
$ php make:seeder <シーダーファイル名>
- 作成されたシーダーファイル内のrunメソッドにデータ挿入処理を記述
<?php
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
public function run()
{
DB::table('users')->delete();
$users = [];
for($i = 1; $i <= 20; $i++){
$users[] = [
'name' => '名前'.$i,
'email' => 'mail'.$i.'@email.com',
'password' => 'pass'.$i
];
}
foreach($users as $user){
DB::table('users')->insert($user);
}
}
}
$ php artisan db:seed
Seeded: UsersTableSeeder
- なにしても動かなかったから下記コマンド実行したら動いためも
$ composer dump-autoload