<?php

namespace App\Models;

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

class ActivityMember extends BaseModel
{
    use HasFactory;

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function orders()
    {
        return $this->hasMany(ActivityOrder::class, 'user_id', 'user_id');
    }

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

    public function agentUser()
    {
        return $this->hasOne(AgentUser::class,"user_id", "user_id");
    }
}
