Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Laravel で Ruby on Rails チュートリアル【6章】

環境

  • Laravel 5.5
  • PHP 7.2

この章でやること

6.1 Userモデル

6.1.1 データベースの移行

既存のUserモデル、create_users_tableマイグレーションを流用する

マイグレーション編集(/database/migrations/[timestamp]create_users_table.php)
```php
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');

        $table->timestamps();
    });
}

public function down()
{
    Schema::dropIfExists('users');
}

}

マイグレーション適用

artisan migrate
```

6.1.2 modelファイル

(/app/User.php)
```php
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
use Notifiable;

protected $table = 'users';

protected $guarded = ["id"];

}
```

6.1.3 ユーザーオブジェクトを作成する

$user = new User;
$user->name = "Michael Hartl";
$user->email = "mhartl@example.com";
$user->save();

$foo = User::create(['name' => "A Nother", 'email' => "another@example.org"]);
$foo->delete();

6.1.4 ユーザーオブジェクトを検索する

User::find(1);
User::where('email', "mhartl@example.com");
User::first();
User::all();

6.1.5 ユーザーオブジェクトを更新する

$user = User::find(1);

$user->email = "mhartl@example.com";
$user->save();

$user->update(["name" => "The Dude", "email" => "dude@abides.org"]);

6.2 ユーザーを検証する

6.2.1 有効性を検証する

6.2.2 存在性を検証する

(/app/Http/Controllers/UsersController.php)
php
public function store(Request $request)
{
$request->validate([
"name" => "required"
,
"email" => "required"
]);
}

6.2.3 長さを検証する

(/app/Http/Controllers/UsersController.php)
php
public function store(Request $request)
{
$request->validate([
"name" => "required|max:50",
"email" => "required|max:255"
]);
}

6.2.4 フォーマットを検証する

(/app/Http/Controllers/UsersController.php)
php
public function store(Request $request)
{
$request->validate([
"name" => "required|max:50",
"email" => "required|max:255|email"
]);
}

6.2.5 一意性を検証する

(/app/Http/Controllers/UsersController.php)
php
public function store(Request $request)
{
$request->validate([
"name" => "required|max:50",
"email" => "required|max:255|email|unique:users|email"
]);
}

マイグレーション作成(database/migrations/[timestamp]_add_index_to_users_email.php)
```php
class AddIndexToUsersEmail extends Migration
{

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->unique('email');
    });
}

public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropUnique('users_email_unique');
    });
}

}
```

6.3 セキュアなパスワードを追加する

6.3.1 ハッシュ化されたパスワード

マイグレーション作成(/database/migrations/[timeatamp]_add_password_digest_to_users.php)
※laravelの処理の関係でremember_tokenもこのタイミングで追加
```php
class AddPasswordDigestToUsers extends Migration
{
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('password');
$table->rememberToken();
});
}

public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropColumn('password');
        $table->dropRememberToken();
    });
}

}
```

6.3.2 ユーザーがセキュアなパスワードを持っている

バリデーションルール修正(/app/Http/Controllers/UsersController.php)
```php
public function store(Request $request)

{
    $request->validate([
        "name" => "required|max:50",
        "email" => "required|max:255|email|unique:users|email",
        "password" => "required|confirmed"
    ]);
}
### 6.3.3 パスワードの最小文字数

バリデーションルールの修正(/app/User.php)
```php
    public function store(Request $request)
    {
        $request->validate([
            "name" => "required|max:50",
            "email" => "required|max:255|email|unique:users|email",
            "password" => "required|confirmed|min:6"
        ]);
    }

6.3.4 ユーザーの作成と認証

tinkerでユーザを作成
php
User.create(["name" => "Michael Hartl", "email" => "mhartl@example.com", "password" => bcrypt("foobar")]);

6.4 最後に

参考

https://qiita.com/kd9951/items/c06d107aa4018295aa25

hiroita
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away