Exemplo n.º 1
0
 /**
  * Find products by category
  *
  * @param  Category $category
  * @param  array    $options
  * @return array
  */
 public static function browseByCategory($category, $options = [])
 {
     $model = new Product();
     $find = $category->products();
     $fillable = $model->fillable;
     if (!empty($options['filters'])) {
         $inFilters = array_intersect($fillable, array_keys($options['filters']));
         foreach ($inFilters as $key) {
             $find = $find->where("shop_products.{$key}", 'LIKE', $options['filters'][$key]);
         }
     }
     $total = $find->count();
     if (!empty($options['order'])) {
         foreach ($options['order'] as $field => $direction) {
             $find = $find->orderBy($field, $direction);
         }
     }
     if (!empty($options['offset'])) {
         $find = $find->skip($options['offset']);
     }
     if (!empty($options['limit'])) {
         $find = $find->take($options['limit']);
     }
     if (!empty($options['cursor'])) {
         $find = $find->where('shop_products.id', '<', $options['cursor']);
     }
     return ['total' => $total, 'offset' => empty($options['offset']) ? 0 : $options['offset'], 'limit' => empty($options['limit']) ? 0 : $options['limit'], 'data' => $find->get()];
 }
 public function testDeleteSuccess()
 {
     $category = factory(Category::class)->create();
     $user = factory(App\User::class)->make(['hasRole' => true]);
     Auth::login($user);
     $res = $this->call('DELETE', "/categories/{$category->id}");
     $this->assertEquals(204, $res->getStatusCode());
     $exists = Category::find($category->id);
     $this->assertNull($exists);
 }