Created
March 9, 2019 12:45
-
-
Save earth774/a2ea6229924c84fb7a2ef2121cb08414 to your computer and use it in GitHub Desktop.
add addData in UserController.php
This file contains hidden or 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; | |
use App\User; | |
use Illuminate\Http\Request; | |
use Illuminate\Http\Response; | |
class UserController extends Controller | |
{ | |
/** | |
* Create a new controller for User. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
// | |
} | |
public function getAll() | |
{ | |
$data = User::all(); | |
return $this->responseSuccess($data); | |
} | |
public function getID($id) | |
{ | |
$data = User::where('id', $id)->first(); | |
return $this->responseSuccess($data); | |
} | |
public function addData(Request $request) | |
{ | |
$data = new User(); | |
$data->name = $request->name; | |
$data->age = $request->age; | |
$data->tel = $request->tel; | |
if ($data->save()) { | |
return $this->responseSuccess($data); | |
} | |
} | |
public function updateData($id) | |
{ | |
return $this->responseSuccess('Update Data' . $id); | |
} | |
public function deleteData($id) | |
{ | |
return $this->responseSuccess('Delete Data' . $id); | |
} | |
protected function responseSuccess($res) | |
{ | |
return response()->json(["status" => "success", "data" => $res], 200) | |
->header("Access-Control-Allow-Origin", "*") | |
->header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment