Skip to content

Instantly share code, notes, and snippets.

@phanngoc
Created October 27, 2015 03:38
Show Gist options
  • Select an option

  • Save phanngoc/e7bb6b15ed7cdd8d07da to your computer and use it in GitHub Desktop.

Select an option

Save phanngoc/e7bb6b15ed7cdd8d07da to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers\Back;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Work;
use Validator;
use App\Image;
class WorkController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$works = Work::paginate(20);
return view('back.works.index', compact('works'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('back.works.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request Request
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'title' => 'required',
'cost' => 'numeric',
'square_metres' => 'numeric',
'thumbnail' => 'required|image',
'before_after_image' => 'image',
'installation_year' => 'required',
'area' => 'required',
'family_structure' => 'required',
'sequence_image.0' => 'required',
], [
'sequence_image.0.required' => 'We need at least 1 image',
]);
if ($validator->fails()) {
return redirect()->route('admin.works.create')->withErrors($validator, 'errorwork')->withInput();
} else {
$work = new Work();
$work->createWork($request);
return redirect()->route('admin.works.index');
}
}
/**
* Display the specified resource.
*
* @param int $id Work id
*
* @return \Illuminate\Http\Response
*/
public function show($id)
{
if (!ctype_digit($id)) {
return redirect()->route('dashboard');
}
$work = Work::find($id);
return view('back.works.edit', compact('work'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id Work id
*
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
return $id;
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request Request
* @param int $id Work id
*
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$work = Work::find($id);
$validator = Validator::make($request->all(), [
'title' => 'required',
'cost' => 'numeric',
'square_metres' => 'numeric',
'thumbnail' => 'image',
'before_after_image' => 'image',
'installation_year' => 'required',
'area' => 'required',
'family_structure' => 'required',
]);
$seqImgIsUploaded = $request->input('img_is_uploaded');
if ($seqImgIsUploaded == null && $request->file('sequence_image')[0] == null) {
$errorMessages = new \Illuminate\Support\MessageBag();
if ($validator->fails()) {
$errorMessages->merge($validator->errors()->toArray());
}
$errorMessages->add('sequence_image', 'You must upload at least 1 image.');
return redirect()->route('admin.works.show', $id)->withErrors($errorMessages, 'errorwork')->withInput();
}
if ($validator->fails()) {
return redirect()->route('admin.works.show', $id)->withErrors($validator, 'errorwork')->withInput();
} else {
$work->updateWork($request);
return redirect()->route('admin.works.show', $work->id);
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id Work id
*
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
Work::destroyData($id);
return redirect()->route('admin.works.index');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment