Skip to content

Instantly share code, notes, and snippets.

@nissicreative
Last active August 31, 2016 18:29
Show Gist options
  • Save nissicreative/31dd9c62408972b96f5a21f28b3d8d56 to your computer and use it in GitHub Desktop.
Save nissicreative/31dd9c62408972b96f5a21f28b3d8d56 to your computer and use it in GitHub Desktop.
Boilerplate for Laravel Admin Controller
<?php
namespace App\Http\Controllers\Admin;
use App\BlogPost;
use Carbon\Carbon;
use Laracasts\Flash\Flash;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redirect;
class BlogController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$posts = BlogPost::orderBy('published_at', 'desc')->paginate(20);
return view('admin.posts.index', compact('posts'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$post = new BlogPost();
$post->published_at = Carbon::now();
return view('admin.posts.create', compact('post'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'content' => 'required'
]);
$post = new BlogPost();
$data = $request->all();
$data['published_at'] = make_date($request->published_at);
$data['slug'] = str_slug($request->title);
$post->fill($data);
$post->save();
Flash::success('The post was created!');
return Redirect::route('admin.posts.index');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$post = BlogPost::findOrFail($id);
return view('admin.posts.show', compact('post'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$post = BlogPost::findOrFail($id);
return view('admin.posts.edit', compact('post'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'title' => 'required',
'content' => 'required'
]);
$post = BlogPost::findOrFail($id);
$data = $request->all();
$data['published_at'] = make_date($request->published_at);
$post->fill($data);
$post->save();
Flash::success('The post has been updated.');
return Redirect::route('admin.posts.index');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$post = BlogPost::findOrFail($id);
$post->delete();
Flash::success('The post has been deleted!');
return Redirect::route('admin.posts.index');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment