Advertisement
Guest User

controller

a guest
Mar 21st, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.19 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use App\bidang as Bidang;
  7. use App\User as user;
  8.  
  9. class Admin extends Controller
  10. {
  11.     protected $rules = [
  12.         'bidang' => 'required'
  13.     ];
  14.  
  15.     /**
  16.      * Display a listing of the resource.
  17.      *
  18.      * @return \Illuminate\Http\Response
  19.      */
  20.     public function index()
  21.     {
  22.    
  23.         $parent = Bidang::with('children')->where('id_parent', '=', 0)->get();
  24.  
  25.         return response()->json($parent);
  26.     }
  27.  
  28.     /**
  29.      * Show the form for creating a new resource.
  30.      *
  31.      * @return \Illuminate\Http\Response
  32.      */
  33.     public function create()
  34.     {
  35.         //
  36.     }
  37.  
  38.     //function add bidang
  39.     public function storeBidang(Request $request)
  40.     {
  41.         if (!is_array($request->all())) {
  42.             return ['error' => 'request harus berbentuk array'];
  43.         }
  44.  
  45.         try{
  46.  
  47.            $validator = \Validator::make($request->all(), $this->rules);
  48.  
  49.            if ($validator->fails()) {
  50.                
  51.                return response()->json([
  52.                     'updated' => false,
  53.                     'errors' => $validator->errors()->all()
  54.                 ], 500);
  55.            }
  56.            else{
  57.  
  58.                 $bidang = new Bidang;
  59.                 $id = 0;  
  60.  
  61.                 $bidang->nama = $request->input('bidang');
  62.                 $bidang->id_parent = $id;
  63.                 $bidang->save();
  64.  
  65.                 return response()->json($bidang, 201);
  66.            }
  67.  
  68.         }
  69.  
  70.         catch(Exception $e){
  71.             \Log::info('Error Creating data bidang');
  72.             return response()->json(['created' => false], 500);
  73.         }
  74.    
  75.     }
  76.  
  77.     //function add subbidang
  78.     public function storeSubBidang(Request $request)
  79.     {
  80.         if (!is_array($request->all())) {
  81.             return ['error' => 'request harus berbentuk array'];
  82.         }
  83.  
  84.         try{
  85.  
  86.            $validator = \Validator::make($request->all(), $this->rules);
  87.  
  88.            if ($validator->fails()) {
  89.                
  90.                return response()->json([
  91.                     'updated' => false,
  92.                     'errors' => $validator->errors()->all()
  93.                 ], 500);
  94.            }
  95.            else{
  96.  
  97.                 $bidang = new Bidang;
  98.                
  99.                 $bidang->id_parent = $request->input('bidang');
  100.                 $bidang->nama = $request->input('subbidang');
  101.                 $bidang->save();
  102.  
  103.                 return response()->json($bidang, 201);
  104.            }
  105.  
  106.         }
  107.  
  108.         catch(Exception $e){
  109.             \Log::info('Error Creating data bidang');
  110.             return response()->json(['created' => false], 500);
  111.         }
  112.    
  113.     }
  114.  
  115.     /**
  116.      * Display the specified resource.
  117.      *
  118.      * @param  int  $id
  119.      * @return \Illuminate\Http\Response
  120.      */
  121.     public function show($id)
  122.     {
  123.         //
  124.     }
  125.  
  126.     /**
  127.      * Show the form for editing the specified resource.
  128.      *
  129.      * @param  int  $id
  130.      * @return \Illuminate\Http\Response
  131.      */
  132.     public function edit($id)
  133.     {
  134.         //
  135.     }
  136.  
  137.     /**
  138.      * Update the specified resource in storage.
  139.      *
  140.      * @param  \Illuminate\Http\Request  $request
  141.      * @param  int  $id
  142.      * @return \Illuminate\Http\Response
  143.      */
  144.     public function updateBidang(Request $request, $id)
  145.     {
  146.  
  147.         if (!is_array($request->all())) {
  148.             return ['error' => 'request harus berbentuk array'];
  149.         }
  150.  
  151.         try{
  152.  
  153.            $validator = \Validator::make($request->all(), $this->rules);
  154.  
  155.            if ($validator->fails()) {
  156.                
  157.                return response()->json([
  158.                     'updated' => false,
  159.                     'errors' => $validator->errors()->all()
  160.                 ], 500);
  161.            }
  162.            else{
  163.  
  164.                 $bidang = Bidang::find($id);
  165.                 $bidang->nama = $request->input('bidang');
  166.                 $bidang->save();
  167.  
  168.                 return response()->json([
  169.                     'data' => $bidang,
  170.                     'updated' => true
  171.                 ],201);
  172.            }
  173.  
  174.         }
  175.  
  176.         catch(Exception $e){
  177.             \Log::info('Error Creating data bidang');
  178.             return response()->json(['created' => false], 500);
  179.         }
  180.     }
  181.  
  182.     //delete bidang
  183.     public function destroyBidang($id)
  184.     {
  185.         $bidang = Bidang::find($id);
  186.         $children = Bidang::find($id)->where('id_parent','=',$id);
  187.         $bidang->delete();
  188.         $children->delete();
  189.  
  190.         return response()->json([
  191.             'status' => 'bidang telah dihapus'
  192.         ]);
  193.     }
  194.  
  195.     public function destroySubBidang($id)
  196.     {
  197.         $children = Bidang::find($id);
  198.         $children->delete();
  199.  
  200.         return response()->json([
  201.             'status' => 'bidang telah dihapus'
  202.         ]);
  203.     }
  204.  
  205.     public function register(Request $request){
  206.  
  207.         $user = new user;
  208.         $user->name = $request->input('nama');
  209.         $user->email = $request->input('email');
  210.         $user->password = $request->input('password');
  211.         $user->save();
  212.  
  213.         return response()->json([
  214.             "message" => 'sukses'
  215.  
  216.         ], 200)
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement