/** * boot * observing model * */ public static function boot() { parent::boot(); ProductExtension::observe(new ProductExtensionObserver()); }
/** * Display all extensions * * @return Response */ public function extensions($type = null) { $result = \App\Models\ProductExtension::active(true); if (Input::has('search')) { $search = Input::get('search'); foreach ($search as $key => $value) { switch (strtolower($key)) { case 'name': $result = $result->name($value); break; default: # code... break; } } } if (Input::has('sort')) { $sort = Input::get('sort'); foreach ($sort as $key => $value) { if (!in_array($value, ['asc', 'desc'])) { return new JSend('error', (array) Input::all(), $key . ' harus bernilai asc atau desc.'); } switch (strtolower($key)) { case 'name': $result = $result->orderby($key, $value); break; } } } $count = count($result->get(['id'])); if (Input::has('skip')) { $skip = Input::get('skip'); $result = $result->skip($skip); } if (Input::has('take')) { $take = Input::get('take'); $result = $result->take($take); } $result = $result->get()->toArray(); return new JSend('success', (array) ['count' => $count, 'data' => $result]); }
/** * Delete a Extension * * @return Response */ public function delete($id = null) { // $extension = \App\Models\ProductExtension::id($id)->with(['images'])->first(); if (!$extension) { return new JSend('error', (array) Input::all(), 'Extension tidak ditemukan.'); } $result = $extension->toArray(); if ($extension->delete()) { return new JSend('success', (array) $result); } return new JSend('error', (array) $result, $extension->getError()); }