cossack910 / CRMSystem

Laravel10 + inertia + vue
0 stars 0 forks source link

クエリスコープ #33

Open cossack910 opened 11 months ago

cossack910 commented 11 months ago

公式 https://readouble.com/laravel/10.x/ja/eloquent.html

cossack910 commented 11 months ago

ローカルスコープの利用

スコープを定義したら、モデルをクエリするときにスコープメソッドを呼び出すことができます。ただし、メソッドを呼び出すときにzscopeプレフィックスを含めないでください。さまざまなスコープに呼び出しをチェーンすることもできます。

CustomerModel

スコープメソッドの命名規則として必ず「scope関数名」とする

class Customer extends Model
{
    use HasFactory;

    public function scopeSearchCustomers($query, $input = null) <-これ
    {
        if(!empty($input)){
            if(Customer::where('kana', 'like', $input.'%')->orWhere('tel', 'like', $input.'%')->exists())
            {
                return $query->where('kana', 'like', $input.'%')->orWhere('tel', 'like', $input.'%');
            }

        }
    }
}

呼び出し側ではscopeを含まない

class CustomerController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $customers = Customer::searchCustomers('キジマ')->select('id', 'name' , 'kana', 'tel')->paginate(50); <-これ
        dd($customers);

        return Inertia::render('Customers/Index', [
            'customers' => Customer::select('id', 'name', 'kana', 'tel')->paginate(50)
        ]);
    }
cossack910 commented 10 months ago

グローバルスコープ

公式(グローバルスコープ欄参照) https://readouble.com/laravel/10.x/ja/eloquent.html

モデル作成

docker exec -it crmsystem php artisan make:model Order

グローバルスコープ用ファイル作成 Modelsの中に Scopeフォルダが作成されSubtotal.phpが作成される。

docker exec -it crmsystem php artisan make:scope Subtotal

App\Models\Scope\Subtotal.php

<?php

namespace App\Models\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class Subtotal implements Scope
{
    /**
     * Apply the scope to a given Eloquent query builder.
     */
    public function apply(Builder $builder, Model $model): void
    {
        $sql = 'select p.id as id
        ,ip.id as pivot_id
        ,i.price * ip.quantity as subtortal
        ,c.name as customer_name
        ,i.name as item_name
        ,i.price as item_price
        ,ip.quantity
        ,p.status
        ,p.created_at
        ,p.updated_at
        from purchases p
        left join item_purchase ip on p.id = ip.purchase_id
        left join items i on ip.item_id = i.id
        left join customers c on p.customer_id = c.id
        ';
        $builder->fromSub($sql, 'order_subtotals');
    }
}

models

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Scopes\Subtotal;

class Order extends Model
{
    use HasFactory;

    protected static function booted()
    {
        static::addGlobalScope(new Subtotal);
    }
}