作成日:2022-11-30, 更新日:2022-12-14
基本
- 「artisan」は「アーティザン」と読むらしい
- コントローラーをはじめファイルを作るときにartisanで何かのコマンドを使うらしい
- 「artisan」で使うコマンドは自作が可能らしい
- 「Laravel Sail」を使っている場合、「sail artisan」とするらしい
- 「artisan」自体はルートにいる(composer.jsonと同じ階層)。「sail」は「vendor/bin」の下にいる
コマンド
▼こんな感じのコマンド
$ php artisan コマンド
▼Laravel Sailを使っている場合
$ ./vendor/bin/sail php artisan コマンド
artisanで使えるコマンド一覧
$ php artisan list
artisanで使えるコマンドのヘルプ
$ php artisan help コマンド
controller関連
$ php artisan make:controller ExampleController
▼作られたファイル: app/Http/Controllers/ExampleController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class ExampleController extends Controller { // }
「Controllers/xxx.php」じゃなく「Controllers/xxx/xxx.php」を作りたい
▼「app/Http/Controllers/Example/ExampleController.php」を作ってほしい
$ php artisan make:controller Example/ExampleController
model関連(データを取得。DBにクエリを投げたりする)
$ php artisan make:model Sample
▼作られたファイル: app/Models/Sample.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Sample extends Model { use HasFactory; }
modelファイルとmigrationsファイルの両方を作成
$ php artisan make:model Sample -m
migrate関連(DBのテーブル作成)
migrationsファイルを作成
- 新規作成
$ php artisan make:migration create_sample_table --create=sample
- 更新
$ php artisan make:migration rename_birth_to_birthdate_on_sample_table --table=sample
▼「テーブル名: sample」「ファイル名: create_sample_table」でマイグレーションファイルを作成したい
$ php artisan make:migration create_sample_table --create=sample
→ファイル名に年月日時分秒が追加され「database/migrations/2022_11_30_051918_create_sample_table.php」が作られる
▼修正されるファイル
vendor\composer\autoload_classmap.php vendor\composer\autoload_static.php
▼作られたファイル: database/migrations/2022_11_30_051918_create_sample_table.php
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('sample', function (Blueprint $table) { $table->id(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('sample'); } }
※「--create=sample」でなく「--table=sample」にすると「Schema::create()」が「Schema::table()」になる
「$table->id()」が何ができるかは知らないけど…未調査。実際に作業に入るときに再調査の予定
※「$table->increments('id')」だと「カラム名: id」、「$table->text('hoge')」だと「カラム名: hoge、text型」ができるっぽい
migrationsを実行関連
▼migrationsを実行
$ php artisan migrate
▼最後のマイグレーションをロールバック
$ php artisan migrate:rollback
▼マイグレーションのすべてをロールバック
$ php artisan migrate:reset
seeder関連(DBのテーブルにデータを流し込む)
シーダファイルの作成
▼「database/seeders/UserSeeder.php」をつくる
$php artisan make:seeder UserSeeder
▼作られたファイル: database/seeders/UserSeeder.php
<?php namespace Database\Seeders; use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; class UserSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { // } }
シーダの実行
▼database/seedersの中のすべてを実行
$ php artisan db:seed
▼database/seeders/UserSeederだけ実行
$ php artisan db:seed --class=UserSeeder
キャッシュのクリア関連
キャッシュのクリア
$ php artisan cache:clear
設定キャッシュのクリア
$ php artisan config:clear
ルーティングのキャッシュクリア
$ php artisan route:clear
Viewのキャッシュクリア
$ php artisan view:clear