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\Http\Requests;
- use App\Http\Controllers\Controller;
- use App\Commodity;
- class CommodityController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return Response
- */
- public function index()
- {
- //
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return Response
- */
- public function create()
- {
- return \View::make('pages.admin.commodity.add');
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param Request $request
- * @return Response
- */
- public function store(Request $request)
- {
- $validator = \Validator::make(\Request::all(),
- ['name' => 'required|unique:commodities,name'],
- ['price' => 'required']
- );
- if(!$validator -> fails()){
- $com = new Commodity;
- $com -> name = \Request::input('name', 'Test');
- $com -> price = \Request::input('price', '0.00');
- $com -> description = \Request::input('description', 'desc');
- $com -> save();
- return redirect('commodities/add') -> with('message', 'Commodity Added!');
- } else {
- return redirect('commodities/add') -> withErrors($validator->errors());
- }
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return Response
- */
- public function show($id)
- {
- if(\Request::ajax()){
- $commodity = Commodity::find($id);
- echo json_encode($commodity);
- }
- }
- /**
- * Display all resource.
- *
- * @return Response
- */
- public function showAll()
- {
- $commodities = Commodity::all();
- return \View::make('pages.admin.commodity.view') -> with('commodities', $commodities);
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return Response
- */
- public function edit($id)
- {
- //
- }
- /**
- * Update the specified resource in storage.
- *
- * @param Request $request
- * @return Response
- */
- public function update(Request $request)
- {
- $validator = \Validator::make(\Request::all(), [
- 'name' => 'required',
- 'price' => 'required'
- ]);
- if(!$validator -> fails()){
- $id = \Request::input('id');
- $com = Commodity::find($id);
- $com -> name = \Request::input('name', 'Test');
- $com -> price = \Request::input('price', '0.00');
- $com -> description = \Request::input('description', 'desc');
- $activity = "Commodity has been modified.";
- $activity .= "<br /> Changes: <br />";
- $activity .= "<ul>";
- $activity .= $com -> isDirty('name') ? "<li>Name: " . $com -> getOriginal('name') . " -> " . \Request::input('name') . "</li>" : "";
- $activity .= $com -> isDirty('price') ? "<li>Name: " . $com -> getOriginal('price') . " -> " . \Request::input('price') . "</li>" : "";
- $activity .= $com -> isDirty('description') ? "<li>Name: " . $com -> getOriginal('description') . " -> " . \Request::input('description') . "</li>" : "";
- $activity .= "</ul>";
- $com -> save();
- \Activity::log($activity);
- return redirect('commodities') -> with('message', 'Commodity Updated!');
- } else {
- return redirect('commodities') -> withErrors($validator->errors());
- }
- }
- /**
- * Remove the specified resource from storage.
- *
- * @return Response
- */
- public function destroy()
- {
- $id = \Request::input('id');
- $c = Commodity::find($id);
- $c -> delete();
- return redirect('commodities') -> with('message', 'Commodity Destroyed!');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement