This repository has been archived by the owner on Feb 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProduct.php
64 lines (54 loc) · 1.58 KB
/
Product.php
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use App\Traits\UsesUuid;
class Product extends Model {
use UsesUuid;
/**
* Los atributos que se pueden asignar al crearse/editarse.
*
* @var array
*/
protected $fillable = [
'name', 'img_src', 'description', 'product_type_id', 'brand_id',
];
/**
* Un producto pertenece a una marca.
*/
public function brand() {
return $this->belongsTo('App\Brand');
}
public function brandName() {
return $this->brand()->first()->name;
}
/**
* Un producto pertenece a un tipo.
*/
public function productType() {
return $this->belongsTo('App\ProductType');
}
public function productTypeName() {
return $this->productType()->first()->name;
}
/**
* Busca productos utilizando una aguja por:
* - nombre
* - tipo de producto
* - marca
*/
public static function searchBy(String $needle) {
return DB::table("products")
->join("brands", "products.brand_id", "=", "brands.id")
->join("product_types", "products.product_type_id", "=", "product_types.id")
->select(
"products.*",
"brands.name as brand_name",
"product_types.name as product_type_name"
)
->where("products.name", "like", "%$needle%")
->orWhere("brands.name", "like", "%$needle%")
->orWhere("product_types.name", "like", "%$needle%")
->get();
}
}