Pada tutorial kali ini kita akan membuat sebuah engine REST-ful untuk delete data, sebagai lanjutan tutorial yang sebelumnya.
Sama seperti tutorial sebelumnya untuk dapat melakukan delete data user maka kita harus melempar parameter ID data user yang akan didelete.
Oke langsung saja kita mulai experimentnya,
Pertama, tambahkan route baru untuk method baru delete ini dengan mengedit code pada file route api.php yang terletak pada path protected/app/routes/ edit menjadi seperti ini,
<?php
//////////
// CRUD //
//////////
//pahamilah group route dibawah
Route::group(array('prefix' => 'crud'), function(){
Route::get('users', 'Controller\Api\UserController@getUsers');
Route::post('store-user', 'Controller\Api\UserController@storeUser');
Route::get('{id}', 'Controller\Api\UserController@showUser');
Route::patch('{id}', 'Controller\Api\UserController@updateUser');
Route::delete('{id}', 'Controller\Api\UserController@deleteUser');
});
Selanjutnya, edit file UserController.php pada path protected/app/controllers/api/ menjadi seperti ini,
<?php namespace Controller\Api;
use Model\User;
use \Response;
use \Validator;
use \Input;
use Illuminate\Http\Request;
class UserController extends \BaseController
{
public function getUsers()
{
$users = User::get();
$response['status'] = 'OK';
$response['result'] = $users;
return Response::json($response);
}
public function storeUser()
{
$validator = Validator::make(
Input::all(),
array(
"name" => "required",
"username" => "required|unique:users,username",
"email" => "required|email|unique:users,email",
"password" => "required|min:6",
)
);
// dd($_POST);
if ($validator->passes()) {
$user = User::storeOrUpdateUser(Input::all(), null);
// dd($_POST);
$response['status'] = 'OK';
$response['result'] = $user;
}
else {
$response['status'] = 'ERROR';
$response['message'] = $validator->messages();
}
return Response::json($response);
}
public function showUser($id)
{
$user = User::find($id);
if ($user) {
$response['status'] = 'OK';
$response['result'] = $user;
} else {
$response['status'] = 'ERROR';
$response['message'] = 'User not found';
}
return Response::json($response);
}
public function updateUser($id)
{
$validator = Validator::make(
Input::all(),
array(
"name" => "required",
"username" => "required|unique:users,username,".$id,
"email" => "required|email|unique:users,email,".$id,
"password" => "required|min:6",
)
);
$user = User::find($id);
if ($user) {
if ($validator->passes()) {
$user = User::storeOrUpdateUser(Input::all(), $user);
$response['status'] = 'OK';
$response['result'] = $user;
}
else {
$response['status'] = 'ERROR';
$response['message'] = $validator->messages();
}
} else {
$response['status'] = 'ERROR';
$response['message'] = 'User not found';
}
return Response::json($response);
}
public function deleteUser($id)
{
$user = User::find($id);
if ($user) {
$user->delete();
$response['status'] = 'OK';
$response['result'] = 'Success delete user';
} else {
$response['status'] = 'ERROR';
$response['message'] = 'User not found';
}
return Response::json($response);
}
}
Terakhir, test pada postman jika ID user ditemukan maka proses delete user berhasil,
Jika ID user tidak ditemukan maka akan muncul error seperti ini,
===DONE!===