Last active
March 1, 2019 18:52
-
-
Save bunlongheng/4bf1c55ebfd08380a5122859dc2a51dd to your computer and use it in GitHub Desktop.
Sample Laravel CRUD Controller
This file contains 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 | |
namespace App\Http\Controllers; | |
use App\ImageWork; | |
use Input, Validator, Auth, Redirect, Request, Session, Mail, View, File, Image, SSH; | |
class ImageController extends Controller { | |
//------------------------------------------------------------------------------------------------- [ Index ] | |
public function index(){ | |
$images = ImageWork::orderBy('created_at', 'desc')->get(); | |
return View::make('layouts.be.images.index', get_defined_vars()); | |
} | |
//------------------------------------------------------------------------------------------------- [ All ] | |
public function all(){ | |
$images = ImageWork::orderBy('created_at', 'desc')->get(); | |
return View::make('layouts.be.images.all', get_defined_vars()); | |
} | |
//------------------------------------------------------------------------------------------------- [ Create] | |
public function create(){ | |
return View::make('layouts.be.images.create', get_defined_vars()); | |
} | |
//------------------------------------------------------------------------------------------------- [ Store ] | |
public function store() | |
{ | |
$inputs = Input::all(); | |
$img_path = array('img_path' => Input::file('img_path')); | |
$rule = ['img_path' => 'max:20000|mimes:jpeg,png']; | |
$validator = Validator::make($img_path, $rule ); | |
if ( $validator->fails()) { | |
return Redirect::to('/image/create')->withErrors($validator)->withInput() | |
->with('error','Something is wrong with your upload image'); | |
} else { | |
$image = new ImageWork; | |
$image->name = $inputs['name']; | |
$image->description = str_replace(' ','-',strtolower($inputs['description'])); | |
$image->save(); | |
$id = $image->id; | |
// SSH::into('production')->run(['date']); | |
$image = ImageWork::find($id); | |
if (Input::hasFile('img_path')){ | |
$path = public_path().'/img/work/'.$id.'/'; | |
$file = Input::file('img_path'); | |
$uploadSuccess = $file->move($path, 'full.jpg'); | |
$file_path = $path.'full.jpg'; | |
$image->img_path = '/img/work/'.$id.'/full.jpg'; | |
$crop_file = Image::make($file_path)->fit(200,150); | |
$crop_file->save($path.'crop.jpg'); | |
} | |
$image->save(); | |
} | |
return Redirect::to('/image') ->with('success','The image was created succesfully!'); | |
} | |
//------------------------------------------------------------------------------------------------- [ Show ] | |
public function show($id) | |
{ | |
$image = ImageWork::findOrFail($id); | |
return View::make('layouts.be.images.show') | |
->with('image', $image); | |
} | |
//------------------------------------------------------------------------------------------------- [ Edit ] | |
public function edit($id) | |
{ | |
$image = ImageWork::findOrFail($id); | |
return View::make('layouts.be.images.edit') | |
->with('image', $image ); | |
} | |
//------------------------------------------------------------------------------------------------- [ Update ] | |
public function update($id) | |
{ | |
$inputs = Input::all(); | |
$img_path = array('img_path' => Input::file('img_path')); | |
$rule = ['img_path' => 'max:20000|mimes:jpeg,png']; | |
$validator = Validator::make($img_path, $rule ); | |
if ( $validator->fails()) { | |
return Redirect::to('/image/update')->withErrors($validator)->withInput() | |
->with('error','Something is wrong with your upload image'); | |
} else { | |
$image = ImageWork::find($id); | |
$image->name = $inputs['name']; | |
$image->description = str_replace(' ','-',strtolower($inputs['description'])); | |
$image->save(); | |
$id = $image->id; | |
$image = ImageWork::find($id); | |
if (Input::hasFile('img_path')){ | |
$path = public_path().'/img/work/'.$id.'/'; | |
$file = Input::file('img_path'); | |
$uploadSuccess = $file->move($path, 'full.jpg'); | |
$file_path = $path.'full.jpg'; | |
$image->img_path = '/img/work/'.$id.'/full.jpg'; | |
$crop_file = Image::make($file_path)->fit(200,150); | |
$crop_file->save($path.'crop.jpg'); | |
} | |
$image->save(); | |
} | |
return Redirect::to('/image') ->with('success','The image was update succesfully!'); | |
} | |
//------------------------------------------------------------------------------------------------- [ Destroy ] | |
public function destroy($id){ | |
$image = ImageWork::find($id); | |
$img_dir = public_path().'/img/work/'.$image->id; | |
if (file_exists($img_dir)) { | |
File::deleteDirectory($img_dir); | |
$image->delete(); | |
return Redirect::to('image') | |
->with('success','The image was deleted succesfully!'); | |
}else{ | |
$image->delete(); | |
return Redirect::to('image') | |
->with('error','The image was not deleted succesfully!'); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment