| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Models; |
| 4 |
|
| 5 |
class Product extends Model |
| 6 |
{ |
| 7 |
protected $table = 'fs_products'; |
| 8 |
|
| 9 |
|
| 10 |
/** |
| 11 |
* The attributes that are mass assignable. |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
protected $fillable = [ |
| 16 |
'source_uid', |
| 17 |
'title', |
| 18 |
'description', |
| 19 |
'settings', |
| 20 |
'source', |
| 21 |
'created_by' |
| 22 |
]; |
| 23 |
|
| 24 |
protected $searchable = ['title', 'description']; |
| 25 |
|
| 26 |
/** |
| 27 |
* Local scope to filter products by search/query string |
| 28 |
* @param ModelQueryBuilder $query |
| 29 |
* @param string $search |
| 30 |
* @return ModelQueryBuilder |
| 31 |
*/ |
| 32 |
|
| 33 |
public function scopeSearchBy($query, $search) |
| 34 |
{ |
| 35 |
if ($search) { |
| 36 |
$fields = $this->searchable; |
| 37 |
$query->where(function ($query) use ($fields, $search) { |
| 38 |
$query->where(array_shift($fields), 'LIKE', "%$search%"); |
| 39 |
|
| 40 |
foreach ($fields as $field) { |
| 41 |
$query->orWhere($field, 'LIKE', "$search%"); |
| 42 |
} |
| 43 |
}); |
| 44 |
} |
| 45 |
|
| 46 |
return $query; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* This `getProducts` method will return the list of products |
| 51 |
* @param string $search |
| 52 |
* @return array |
| 53 |
*/ |
| 54 |
public function getProducts ( $search ) |
| 55 |
{ |
| 56 |
return [ |
| 57 |
'products' => $this->orderBy('id', 'DESC')->searchBy($search)->paginate() |
| 58 |
]; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* This `getProduct` method will get product by id and return |
| 63 |
* @param int $productId |
| 64 |
* @return array |
| 65 |
*/ |
| 66 |
public function getProduct ( $productId ) |
| 67 |
{ |
| 68 |
return [ |
| 69 |
'product' => $this->findOrFail($productId) |
| 70 |
]; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* This `createProduct` method will create a new product |
| 75 |
* @param array $data |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
public function createProduct ( $data ) |
| 79 |
{ |
| 80 |
$data = wp_unslash($data); |
| 81 |
$product = $this->create($data); |
| 82 |
|
| 83 |
return [ |
| 84 |
'message' => __('Product has been successfully created', 'fluent-support'), |
| 85 |
'product' => $product |
| 86 |
]; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* This `updateProduct` method will update an exiting product by id |
| 91 |
* @param int $productId |
| 92 |
* @param array $data |
| 93 |
* @return array |
| 94 |
*/ |
| 95 |
public function updateProduct ( $productId, $data ) |
| 96 |
{ |
| 97 |
$data = wp_unslash($data); |
| 98 |
$product = $this->findOrFail($productId); |
| 99 |
$product->fill($data); |
| 100 |
$product->save(); |
| 101 |
|
| 102 |
return [ |
| 103 |
'message' => __('Product has been updated', 'fluent-support'), |
| 104 |
'product' => $product |
| 105 |
]; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* This `deleteProduct` method will delete an exiting product by id |
| 110 |
* @param int $productId |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
public function deleteProduct ( $productId ) |
| 114 |
{ |
| 115 |
$this->where('id', $productId)->delete(); |
| 116 |
|
| 117 |
return [ |
| 118 |
'message' => __('Product has been deleted', 'fluent-support') |
| 119 |
]; |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
|