Created
June 24, 2018 14:53
-
-
Save heryvandoro/ed442a0ae1525105b8a3913306f0560e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\API; | |
use Illuminate\Http\Request; | |
use App\Http\Controllers\Controller; | |
use Validator; | |
use App\Helpers\MessagesConstants; | |
class BaseAPIController extends Controller | |
{ | |
/** | |
* Display a listing of the resource. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
protected static $model; | |
public function index() | |
{ | |
return self::$model->paginate(10); | |
} | |
/** | |
* Show the form for creating a new resource. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function create() | |
{ | |
// | |
} | |
/** | |
* Store a newly created resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return \Illuminate\Http\Response | |
*/ | |
public function store(Request $request) | |
{ | |
$valid = Validator::make($request->all(), self::$model->rules(), self::$model->messages()); | |
if($valid->fails()){ | |
return response()->json(["errors"=>$valid->errors()]); | |
} | |
$data = new self::$model(); | |
foreach($request->all() as $key=>$val){ | |
$data->$key = $val; | |
} | |
$data->save(); | |
return response()->json(["messages"=>MessagesConstants::$SUCCESS]); | |
} | |
/** | |
* Display the specified resource. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function show($id) | |
{ | |
$data = self::$model->find($id); | |
if($data==NULL) return response()->json(["messages"=>MessagesConstants::$NOT_FOUND]); | |
return $data; | |
} | |
/** | |
* Show the form for editing the specified resource. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function edit($id) | |
{ | |
// | |
} | |
/** | |
* Update the specified resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function update(Request $request, $id) | |
{ | |
$valid = Validator::make($request->all(), self::$model->rules(), self::$model->messages()); | |
if($valid->fails()){ | |
return response()->json(["errors"=>$valid->errors()]); | |
} | |
$data = self::$model()->find($id); | |
foreach($request->all() as $key=>$val){ | |
$data->$key = $val; | |
} | |
$data->save(); | |
return response()->json(["messages"=>MessagesConstants::$SUCCESS]); | |
} | |
/** | |
* Remove the specified resource from storage. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function destroy($id) | |
{ | |
$data = self::$model->find($id); | |
if($data==null){ | |
return response()->json(["messages"=>MessagesConstants::$NOT_FOUND]); | |
}else{ | |
$data->delete(); | |
return response()->json(["messages"=>MessagesConstants::$SUCCESS]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment