0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Laravel備忘

Last updated at Posted at 2023-12-06

自分用備忘も兼ねた内容です。
Laravelのバージョンは10(10.23.1)となります。

プロジェクトの作成

Composerインストール
Node.jsインストール
PowerShellなどで、プロジェクトを作成したいフォルダに移動後

D:\>cd laravel
composer create-project laravel/laravel sample-project --prefer-dist

ついでにNode.jsのモジュールもインストール

npm install

開発用組み込みサーバーを起動

PowerShellなどのコンソールで、プロジェクトを作成したいフォルダに移動後 php artisan serve
http://127.0.0.1:8000/ にアクセス

D:\>cd laravel
D:\laravel> cd sample
D:\laravel\sample> php artisan serve

無題.png

マイグレーション DBの自動構築

プロジェクト内の database > migrations内のPHPファイルに基づき作成

マイグレーションファイルの作成

php artisan make:migration create_flights_table

databese内に以下ファイルが作成される

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('flights');
    }
};

編集する(例)

        Schema::create('flights', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('airline');
            $table->integer('price')->nullable(true);;
            $table->boolean('flag')->nullable(true);;
            $table->timestamps();
        });

未処理のマイグレーションを実行

php artisan migrate

テーブル・カラム等確認

php artisan db:table

//構築しなおす場合(データベースの中身は空に)

php artisan migrate:refresh

コントローラーの作成

コンソールでプロジェクトのフォルダに移動後

php artisan make:controller SampleController

モデルの作成

コンソールでプロジェクトのフォルダに移動後

php artisan make:model Sample

基本

//ルート部分
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\コントローラー名;

Route::get('/',[コントローラー名::class, 'コントローラ内のfunction名']);
Route::post('/user', [UserController::class, 'index']);
Route::match(['get', 'post'], [UserController::class, 'index']); //get post 両方に対応
Route::redirect('/here', '/there'); //リダイレクトさせる場合
Route::view('/welcome', 'welcome'); //コントローラーを経由せずViewを表示
//コントローラ部分
namespace App\Http\Controllers;

use App\Models\モデル名;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;

class コントローラ名 extends Controller
{
    public function function名(Request $request)
    {
        $クラス名 = new モデル名();
        $users = $クラス名 -> メソッド名;
        
        return view('userindex', ['users' => $users]);
    }
    
    public function function名(Request $request)
    {
        //リダイレクトする場合
        return redirect('リダイレクト先');
    }
}
//モデル部分
namespace App\Models;

use Illuminate\Database\Eloquent\Models;
use Illuminate\Database\Eloquent\Builder;

class クラス名 extends Model
{
    public $変数 = ;

    public function ファンクション名(引数)
    {
        return 変数;
    }
}

GET POST内容の受け取り

public funtion ファンクション名(Request $request)
{
    $変数 = $request -> input('フォーム内で指定した名前');
}

SESSION セッションの取り扱い

public funtion ファンクション名(Request $request)
{
    //セッションの内容を変数に入れる
    if (!empty($request -> session() -> get('セッション名')))
    {
        $変数 = $request -> session() -> get('セッション名');
    }

    //セッションに入れる
    $request -> session() -> put('セッション名',内容);
}

DB操作 クエリビルダ

use Illuminate\Support\Facades\DB;

$items = DB::table('テーブル名') -> SELECT('利用するカラム名たち')
    -> WHERE('カラム名','内容')
    -> WHERE('条件を増やす場合のカラム名','内容')
    -> get();

//一件だけ抽出
$items = DB::table('テーブル名') -> WHERE('カラム名','内容') -> first();

//一件INSERT
$insert_naiyo = ['namae'=>'yamada' ,'age'=>'40'];
DB::table('テーブル名') -> insert
([
    $insert_naiyo
]);

//WHEREを満たすレコードをUPDATE
$update_naiyo = ['age' => '40','address' => 'tokyo'];
DB::table('テーブル名') -> WHERE('カラム名','内容') -> update($update_naiyo);

//WHEREを満たすレコードをDELETE
DB::table('テーブル名') -> WHERE('カラム名','内容') -> delete();

//ORDER BY
$books = DB::table('books')->orderBy('updated_at','desc')->get();

//抽出結果が空かどうか判定 空の場合にTrueとなる
if($books->isEmpty()){
    return redirect('/');
}

データベース INNER JOIN

$users = DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();

データベースのUNION

$first = DB::table('users')
            ->whereNull('first_name');

$users = DB::table('users')
            ->whereNull('last_name')
            ->union($first)
            ->get();

素のSQLを実行

$users = DB::table('users')
             ->select(DB::raw('count(*) as user_count, status'))
             ->where('status', '<>', 1)
             ->groupBy('status')
             ->get();
             
//あるいはselectRawを使う
$orders = DB::table('orders')
                ->selectRaw('price * ? as price_with_tax', [1.0825])
                ->get();

Laravelでバルクアップデート upsertの利用

//更新内容の配列を用意 以下は例 
$update_naiyo = [];
foreach($a as $v){
	//データ一件分を連想配列で用意
	$data['mem_cd']  = $v['mem_cd'];
	$data['namae']   = $v['namae'];
	$data['address'] = $v['address'];
	$data['tel']     = $v['tel'];

	//更新したいデータ一件をに追加し、配列をつくっていく
	$update_naiyo[] = $data;
	$data = [];//空にする
}

//バルクアップデート(upsert)
DB::table(table名) -> upsert(
	$update_naiyo,
	['mem_cd','namae'],
	['address','tel']	
);
//upsert部分の二つ目の引数は、条件 WHERE=の項目を意味し、すべて一致すればUPDATE、なければINSERTされる。
//三つ目の引数が、更新あるいは新規作成されるカラム

Laravelでバリデーション 整数 文字列

$validated = $request->validate([
    'content1' => ['integer','min:1','max:9'],  //整数であり、1以上、9以下か
    'content2' => ['string'], //文字列か
]);

バリデーションエラー表示部分(blade内に)

//略
@if ($errors->any())
    <div>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif 

テストの実行

コンソールでプロジェクトのフォルダに移動後

php artisan test

→testフォルダ内のテストがすべて実行され、結果が表示される

Models\Sample.php
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Sample extends Model
{
    use HasFactory;

    public function add_a_and_b ($a,$b)
    {
        return $a + $b;
    }
}
tests\Feature\Test.php
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Sample;

class Test extends TestCase
{
    public function test_sample1(): void
    {
        $response = $this->get('/');
        $response->assertStatus(200);
    }
    public function test_add_a_and_b(): void
    {
        $sample = new Sample;
        $this -> assertSame ( 6 , $sample -> add_a_and_b ( 1 ,5));
    }
}

の場合

PASS  Tests\Feature\Test
✓ sample1      0.01s  
✓ add a and b  0.01s  

と表示される

seeder 適当なデータをデータベースに自動作成

シーダーファイルを作成する

php artisan make:seeder PeopleTableSeeder

データベース挿入文をrunメソッドに追加

database > seeders > PeopleTableSeeder.php
<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class PeopleTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        DB::table('users')->insert([
            'name' => Str::random(10),
            'email' => Str::random(10).'@example.com',
            'password' => Hash::make('password'),
        ]);
    }
}

マイグレーションとシーディング

php artisan migrate:fresh --seed --seeder=PeopleTableSeeder
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?