summaryrefslogtreecommitdiff
path: root/workbench/app/Models/Product.php
blob: 1edb185adf7cb99435e9c082daede8fb3184e0eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php

namespace Workbench\App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Product extends Model
{
    use HasFactory;

    protected static function newFactory()
    {
        return \Workbench\Database\Factories\ProductFactory::new();
    }

    public function scopeSearch(Builder $q, string $s): void
    {
        $s = "%{$s}%";
        $q->where(fn(Builder $q) => (
            $q->orWhere('name', 'like', $s)
              ->orWhere('description', 'like', $s)
        ));
    }

    public function category(): BelongsTo
    {
        return $this->belongsTo(Category::class);
    }
}