searchable; $query->where(function ($query) use ($fields, $search) { $query->where(array_shift($fields), 'LIKE', "%$search%"); foreach ($fields as $field) { $query->orWhere($field, 'LIKE', "$search%"); } }); } return $query; } /** * Local scope to order products alphabetically by title * @param $query * @return mixed */ public function scopeOrderedByTitle($query) { return $query->orderBy('title', 'ASC'); } /** * This `getProducts` method will return the list of products * @param string $search * @return array */ public function getProducts ( $search ) { return [ 'products' => $this->orderedByTitle()->searchBy($search)->paginate() ]; } /** * This `getProduct` method will get product by id and return * @param int $productId * @return array */ public function getProduct ( $productId ) { return [ 'product' => $this->findOrFail($productId) ]; } /** * This `createProduct` method will create a new product * @param array $data * @return array */ public function createProduct ( $data ) { $data = wp_unslash($data); $product = $this->create($data); return [ 'message' => __('Product has been successfully created', 'fluent-support'), 'product' => $product ]; } /** * This `updateProduct` method will update an exiting product by id * @param int $productId * @param array $data * @return array */ public function updateProduct ( $productId, $data ) { $data = wp_unslash($data); $product = $this->findOrFail($productId); $product->fill($data); $product->save(); return [ 'message' => __('Product has been updated', 'fluent-support'), 'product' => $product ]; } /** * This `deleteProduct` method will delete an exiting product by id * @param int $productId * @return array */ public function deleteProduct ( $productId ) { $this->where('id', $productId)->delete(); return [ 'message' => __('Product has been deleted', 'fluent-support') ]; } }