Created
May 17, 2014 08:42
-
-
Save wayanjimmy/b38a9e05669d244db0e5 to your computer and use it in GitHub Desktop.
RoomController Example Laravel
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 | |
class RoomController extends BaseController { | |
public function index() | |
{ | |
$rooms = Room::all(); | |
return View::make('admin.room.index', compact('rooms')); | |
} | |
public function create() | |
{ | |
return View::make('admin.room.create'); | |
} | |
public function store() | |
{ | |
$validator = Validator::make(Input::all(), array( | |
'name' => 'required', | |
'description' => 'required', | |
'public_price' => 'required|numeric', | |
'real_price' => 'required|numeric' | |
)); | |
if($validator->fails()) { | |
// Error | |
$messages = $validator->messages()->all(); | |
return Response::json(['type' => 'error', 'msg' => $messages]); | |
}else{ | |
$room = new Room(); | |
$room->name = Input::get('name'); | |
$room->link = Input::get('name'); | |
$room->description = Input::get('description'); | |
$room->public_price = Input::get('public_price'); | |
$room->real_price = Input::get('real_price'); | |
$room->images = Input::get('images'); | |
if($room->save()) { | |
return Response::json(['type' => 'success']); | |
}else{ | |
return Response::json(['type' => 'error', 'msg' => 'Something wrong']); | |
} | |
} | |
} | |
public function show($id) | |
{ | |
} | |
public function edit($id) | |
{ | |
$room = Room::find($id); | |
return View::make('admin.room.edit', compact('room')); | |
} | |
public function update($id) | |
{ | |
$validator = Validator::make(Input::all(), array( | |
'name' => 'required', | |
'description' => 'required', | |
'public_price' => 'required|numeric', | |
'real_price' => 'required|numeric' | |
)); | |
if($validator->fails()) { | |
// Error | |
$messages = $validator->messages()->all(); | |
return Response::json(['type' => 'error', 'msg' => $messages]); | |
}else{ | |
$room = Room::find($id); | |
$room->name = Input::get('name'); | |
$room->link = Input::get('name'); | |
$room->description = Input::get('description'); | |
$room->public_price = Input::get('public_price'); | |
$room->real_price = Input::get('real_price'); | |
$room->images = Input::get('images'); | |
if($room->save()) { | |
return Response::json(['type' => 'success']); | |
}else{ | |
return Response::json(['type' => 'error', 'msg' => 'Something wrong']); | |
} | |
} | |
} | |
public function destroy($id) | |
{ | |
$room = Room::find($id); | |
if($room->delete()) { | |
return Response::json(['type' => 'success', 'msg' => 'Successfully deleted']); | |
}else{ | |
return Response::json(['type' => 'error', 'msg' => 'Something wrong']); | |
} | |
} | |
public function uploadImage() | |
{ | |
$file = Input::file('image'); | |
$destinationPath = Room::$destinationPath; | |
$filename = $file->getClientOriginalName(); | |
$uploadSuccess = $file->move(public_path() . $destinationPath, $filename); | |
if($uploadSuccess) { | |
return Response::json(['type' => 'success', 'full_filename' => asset($destinationPath.$filename), 'filename' => $filename]); | |
}else{ | |
return Response::json(['type' => 'error', 'msg' => 'Something wrong']); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment