Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\bidang as Bidang;
- use App\User as user;
- class Admin extends Controller
- {
- protected $rules = [
- 'bidang' => 'required'
- ];
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- $parent = Bidang::with('children')->where('id_parent', '=', 0)->get();
- return response()->json($parent);
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- //
- }
- //function add bidang
- public function storeBidang(Request $request)
- {
- if (!is_array($request->all())) {
- return ['error' => 'request harus berbentuk array'];
- }
- try{
- $validator = \Validator::make($request->all(), $this->rules);
- if ($validator->fails()) {
- return response()->json([
- 'updated' => false,
- 'errors' => $validator->errors()->all()
- ], 500);
- }
- else{
- $bidang = new Bidang;
- $id = 0;
- $bidang->nama = $request->input('bidang');
- $bidang->id_parent = $id;
- $bidang->save();
- return response()->json($bidang, 201);
- }
- }
- catch(Exception $e){
- \Log::info('Error Creating data bidang');
- return response()->json(['created' => false], 500);
- }
- }
- //function add subbidang
- public function storeSubBidang(Request $request)
- {
- if (!is_array($request->all())) {
- return ['error' => 'request harus berbentuk array'];
- }
- try{
- $validator = \Validator::make($request->all(), $this->rules);
- if ($validator->fails()) {
- return response()->json([
- 'updated' => false,
- 'errors' => $validator->errors()->all()
- ], 500);
- }
- else{
- $bidang = new Bidang;
- $bidang->id_parent = $request->input('bidang');
- $bidang->nama = $request->input('subbidang');
- $bidang->save();
- return response()->json($bidang, 201);
- }
- }
- catch(Exception $e){
- \Log::info('Error Creating data bidang');
- return response()->json(['created' => false], 500);
- }
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- //
- }
- /**
- * 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 updateBidang(Request $request, $id)
- {
- if (!is_array($request->all())) {
- return ['error' => 'request harus berbentuk array'];
- }
- try{
- $validator = \Validator::make($request->all(), $this->rules);
- if ($validator->fails()) {
- return response()->json([
- 'updated' => false,
- 'errors' => $validator->errors()->all()
- ], 500);
- }
- else{
- $bidang = Bidang::find($id);
- $bidang->nama = $request->input('bidang');
- $bidang->save();
- return response()->json([
- 'data' => $bidang,
- 'updated' => true
- ],201);
- }
- }
- catch(Exception $e){
- \Log::info('Error Creating data bidang');
- return response()->json(['created' => false], 500);
- }
- }
- //delete bidang
- public function destroyBidang($id)
- {
- $bidang = Bidang::find($id);
- $children = Bidang::find($id)->where('id_parent','=',$id);
- $bidang->delete();
- $children->delete();
- return response()->json([
- 'status' => 'bidang telah dihapus'
- ]);
- }
- public function destroySubBidang($id)
- {
- $children = Bidang::find($id);
- $children->delete();
- return response()->json([
- 'status' => 'bidang telah dihapus'
- ]);
- }
- public function register(Request $request){
- $user = new user;
- $user->name = $request->input('nama');
- $user->email = $request->input('email');
- $user->password = $request->input('password');
- $user->save();
- return response()->json([
- "message" => 'sukses'
- ], 200)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement