<?php

namespace App\Models;

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

class S2ShopSpu extends Model
{
    use HasFactory;

    public $table = "s2_shop_spu";

    public function scopeKeyword($query)
    {
        $keyword = request()->keyword;
        if ($keyword) {
            return $query->where(function($sql) use($keyword) {
                $sql->where("title", 'like', "%$keyword%");
            });
        }
        return $query;
    }

    public function shopFood()
    {
        return $this->hasOne(Food::class, 'shop_id', 'id');
    }

    public function shopSku()
    {
        return $this->hasOne(S2ShopSku::class, 'id', 'skuid');
    }


    public function scopeFoodType($query)
    {
        $type = request()->type;
        if ($type) {
            return $query->where(function($sql) use($type) {
                $sql->where("type", $type);
            });
        }
        return $query->whereHas("shopFood");
    }
}
