<?php

namespace Modules\User\Traits;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Modules\User\Models\User;

trait BelongsToUser
{

    /**
     * Notes   : 所属用户
     *
     * @Date   : 2021/3/15 1:09 下午
     * @Author : <Jason.C>
     * @return BelongsTo
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Notes   : 用户 作用域查询
     *
     * @Date   : 2021/6/21 8:57 上午
     * @Author : <Jason.C>
     * @param  Builder  $query
     * @param  User  $user
     * @return Builder
     */
    public function scopeByUser(Builder $query, User $user): Builder
    {
        return $query->where('user_id', $user->getKey());
    }

    /**
     * Notes   : 用户ID 作用域查询
     *
     * @Date   : 2021/6/21 9:04 上午
     * @Author : <Jason.C>
     * @param  Builder  $query
     * @param  int  $userId
     * @return Builder
     */
    public function scopeByUserId(Builder $query, int $userId): Builder
    {
        return $query->where('user_id', $userId);
    }

}
