Advertisement
Guest User

Untitled

a guest
Oct 20th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.03 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6.  
  7. use App\Http\Requests;
  8. use App\Http\Controllers\Controller;
  9.  
  10. use App\Commodity;
  11.  
  12. class CommodityController extends Controller
  13. {
  14.     /**
  15.      * Display a listing of the resource.
  16.      *
  17.      * @return Response
  18.      */
  19.     public function index()
  20.     {
  21.         //
  22.     }
  23.  
  24.     /**
  25.      * Show the form for creating a new resource.
  26.      *
  27.      * @return Response
  28.      */
  29.     public function create()
  30.     {
  31.         return \View::make('pages.admin.commodity.add');
  32.     }
  33.  
  34.     /**
  35.      * Store a newly created resource in storage.
  36.      *
  37.      * @param  Request  $request
  38.      * @return Response
  39.      */
  40.     public function store(Request $request)
  41.     {
  42.         $validator = \Validator::make(\Request::all(),
  43.           ['name' => 'required|unique:commodities,name'],
  44.           ['price' => 'required']
  45.         );
  46.  
  47.         if(!$validator -> fails()){
  48.             $com = new Commodity;
  49.  
  50.             $com -> name = \Request::input('name', 'Test');
  51.             $com -> price = \Request::input('price', '0.00');
  52.             $com -> description = \Request::input('description', 'desc');
  53.  
  54.             $com -> save();
  55.  
  56.             return redirect('commodities/add') -> with('message', 'Commodity Added!');
  57.         } else {
  58.             return redirect('commodities/add') -> withErrors($validator->errors());
  59.         }
  60.     }
  61.  
  62.     /**
  63.      * Display the specified resource.
  64.      *
  65.      * @param  int  $id
  66.      * @return Response
  67.      */
  68.     public function show($id)
  69.     {
  70.         if(\Request::ajax()){
  71.             $commodity = Commodity::find($id);
  72.  
  73.             echo json_encode($commodity);
  74.         }
  75.     }
  76.  
  77.     /**
  78.      * Display all resource.
  79.      *
  80.      * @return Response
  81.      */
  82.     public function showAll()
  83.     {
  84.         $commodities = Commodity::all();
  85.  
  86.         return \View::make('pages.admin.commodity.view') -> with('commodities', $commodities);
  87.     }
  88.  
  89.     /**
  90.      * Show the form for editing the specified resource.
  91.      *
  92.      * @param  int  $id
  93.      * @return Response
  94.      */
  95.     public function edit($id)
  96.     {
  97.         //
  98.     }
  99.  
  100.     /**
  101.      * Update the specified resource in storage.
  102.      *
  103.      * @param  Request  $request
  104.      * @return Response
  105.      */
  106.     public function update(Request $request)
  107.     {
  108.         $validator = \Validator::make(\Request::all(), [
  109.             'name' => 'required',
  110.             'price' => 'required'
  111.         ]);
  112.  
  113.         if(!$validator -> fails()){
  114.             $id  = \Request::input('id');
  115.             $com = Commodity::find($id);
  116.  
  117.             $com -> name = \Request::input('name', 'Test');
  118.             $com -> price = \Request::input('price', '0.00');
  119.             $com -> description = \Request::input('description', 'desc');
  120.  
  121.             $activity = "Commodity has been modified.";
  122.             $activity .= "<br /> Changes: <br />";
  123.             $activity .= "<ul>";
  124.  
  125.             $activity .= $com -> isDirty('name') ? "<li>Name: " . $com -> getOriginal('name') . " -> " . \Request::input('name') . "</li>" : "";
  126.             $activity .= $com -> isDirty('price') ? "<li>Name: " . $com -> getOriginal('price') . " -> " . \Request::input('price') . "</li>" : "";
  127.             $activity .= $com -> isDirty('description') ? "<li>Name: " . $com -> getOriginal('description') . " -> " . \Request::input('description') . "</li>" : "";
  128.  
  129.             $activity .= "</ul>";            
  130.  
  131.             $com -> save();
  132.             \Activity::log($activity);
  133.  
  134.             return redirect('commodities') -> with('message', 'Commodity Updated!');
  135.         } else {
  136.             return redirect('commodities') -> withErrors($validator->errors());
  137.         }
  138.     }
  139.  
  140.     /**
  141.      * Remove the specified resource from storage.
  142.      *
  143.      * @return Response
  144.      */
  145.     public function destroy()
  146.     {
  147.         $id = \Request::input('id');
  148.         $c = Commodity::find($id);
  149.         $c -> delete();
  150.  
  151.         return redirect('commodities') -> with('message', 'Commodity Destroyed!');
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement