LoginSignup
2
3

More than 3 years have passed since last update.

(メモ) Laravel 8入門

Last updated at Posted at 2020-09-22

インストール

composerのインストール
wget -O composer-setup.php "https://getcomposer.org/installer"
php composer-setup.php
rm composer-setup.php
laravelのインストール
composer global require laravel/installer

# 以下に laravel コマンドが入る。PATHに追加しておく
$HOME/.config/composer/vendor/bin/
# プロジェクトの作成
laravel new blog
cd blog
# ビルトインwebサーバー
php artisan serve --host 0.0.0.0 --port 8080

認証

Laravel Jetstream を有効にする

# 認証を含むアプリケーションの生成
laravel new kitetail --jet

cd kitetail
# sqliteのdatabase作成
touch database/database.sqlite
.env
- DB_CONNECTION=mysql
+ DB_CONNECTION=sqlite
+ DB_DATABASE=/absolute/path/to/database/database.sqlite
# 外部キー制約を有効にする(sqlite3では必要)
+ DB_FOREIGN_KEYS=true
テーブルの作成
php artisan migrate

node.js v15 をインストール

  • 認証に必要な様子。最新版を入れるのにはnを使う。
sudo npm install -g n
n --latest
# インストール
sudo n latest

# パッケージのバージョンアップ
sudo npm update -g
sudo npm install && sudo npm run dev

メール送信の有効化

  • .envを環境に合わせて修正する
.env
MAIL_HOST=
MAIL_PORT=2525
MAIL_FROM_ADDRESS=

アカウント登録にメールアドレスの確認を追加

app/Models/User.php
  <?php

  namespace App\Models;
  //
+ use Illuminate\Contracts\Auth\MustVerifyEmail;
  use Illuminate\Database\Eloquent\Factories\HasFactory;
  use Illuminate\Foundation\Auth\User as Authenticatable;
  use Illuminate\Notifications\Notifiable;
  use Laravel\Fortify\TwoFactorAuthenticatable;
  use Laravel\Jetstream\HasProfilePhoto;
  use Laravel\Jetstream\HasTeams;
  use Laravel\Sanctum\HasApiTokens;
  // 
- class User extends Authenticatable
+ class User extends Authenticatable implements MustVerifyEmail
  {
  • routes/web.php の末尾に以下追記
routes/web.php
# メール確認 START ------------------------------
use Illuminate\Foundation\Auth\EmailVerificationRequest;
use Illuminate\Http\Request;

Route::get('/email/verify/{id}/{hash}', function (EmailVerificationRequest $request) {
    $request->fulfill();
    return redirect('/dashboard');
})->middleware(['auth', 'signed'])->name('verification.verify');

Route::get('/email/verify', function () {
    return view('auth.verify-email');
})->middleware(['auth'])->name('verification.notice');

Route::post('/email/verification-notification', function (Request $request) {
    $request->user()->sendEmailVerificationNotification();
    return back()->with('status', 'verification-link-sent');
})->middleware(['auth', 'throttle:6,1'])->name('verification.send');
# メール確認 END ------------------------------

コマンドの作成

コマンドの作成
php artisan make:command SampleCommand
  • Kernel.phpに登録
app/Console/Kernel.php
    protected $commands = [
+       Commands\SampleCommand::class
    ];
登録されているか確認
php artisan list | grep "command:name"
編集
    public function handle()
    {
+       echo "hi ";
        return 0;
    }
実行
php artisan command:name

引数

  • {変数名} を追加すると、引数がなければエラーになる。
app/Console/Commands/SampleCommand.php
-   protected $signature = 'command:name';

    # 引数は必ず必要
+   protected $signature = 'command:name {arg1}';

    # 引数がなくても良くする。
+   protected $signature = 'command:name {arg1?}';

    # 引数がない場合の初期値
+   protected $signature = 'command:name {arg1=user1}';

    # 引数の説明
+   protected $signature = 'command:name {name=laravel : 名前を指定} {age? : 年齢を指定}';
# エラーになる
php artisan command:name

# エラーにならなくなる。
php artisan command:name aa
  • コマンドライン引数の取得
app/Console/Commands/SampleCommand.php
    public function handle()
    {
        echo "hi ";
+       $arg1 = $this->argument("arg1");
+       echo $arg1;
        return 0;
    }

オプションの指定

# オプションの指定
protected $signature = 'command:name {--dry-run}';

# 実行
php artisan command:name --dry-run

## オプション取得
$dry_run = $this->option("dry-run"); // true or false
# オプションの値を取得方法
protected $signature = 'command:name {--greeting=}';

# 実行
php artisan command:name --greeting=Hello

## オプションの値を取得
$greeting = $this->option("greeting"); // Hello
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3