<?php

namespace Modules\Mall\Models\Traits;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Modules\Mall\Models\Shop;

trait BelongsToShop
{

    /**
     * Notes   : 隶属店铺
     * @Date   : 2021/5/6 2:49 下午
     * @Author : < Jason.C >
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function shop(): BelongsTo
    {
        return $this->belongsTo(Shop::class);
    }

    /**
     * Notes   : 作用域查询
     * @Date   : 2021/6/21 8:57 上午
     * @Author : < Jason.C >
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @param  \Modules\Mall\Models\Shop              $shop
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeByShop(Builder $query, Shop $shop): Builder
    {
        return $query->where('shop_id', $shop->getKey());
    }

    /**
     * Notes   : 店铺ID 作用域查询
     * @Date   : 2021/6/21 9:04 上午
     * @Author : < Jason.C >
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @param  int                                    $shopId
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeByShopId(Builder $query, int $shopId): Builder
    {
        return $query->where('shop_id', $shopId);
    }

}