You are on page 1of 14

Laravel Blog

//web.php

Route::group(['prefix' => 'admin], function () {

Route::resource('posts', 'admin\AdminPostController', ['as' => 'admin']);

});

//admin/_layouts/admin.blade.php

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

<script src="{{ asset('js/bootstrap.min.js') }}"></script>

<link href="{{ asset('css/bootstrap.min.css')}}" rel="stylesheet">

<script type="text/javascript"
src="{{ asset('/js/tinymce/tinymce.min.js') }}"></script>

<script>tinymce.init({

selector : "textarea",

toolbar : "insertfile undo redo | styleselect | bold italic | alignleft


aligncenter alignright alignjustify | bullist numlist outdent indent | link image
jbimages",

});</script>

<title>My Awesome Admin Panel</title>

{{ Html::style('css/admin.css') }}

<!--[if lt IE 9]>

<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script

<![endif]-->

</head>

<body>

<header>

<div class="container">
<h1>My Awesome Admin Panel</h1>

</div>

</header>

<main class="container">

@yield('content')

</main>

<footer>

<div class="container">

&copy; {{ date('Y') }} My Awesome Company

</div>

</footer>

</body>

</html>

//views/admin/post/index.blade.php

@extends('admin._layouts.admin')

@section('content')

<h1>Posts</h1>

{{ link_to_route('admin.posts.create', 'Create new Post') }}

@if(count($posts))

<ul>

@foreach($posts as $post)

<li>

{{ link_to_route('admin.posts.edit', $post->title, array($post->id)) }}

{{ Form::open(array('route' => array('admin.posts.destroy', $post->id),


'method' => 'delete', 'class' => 'destroy')) }}

{{ Form::submit('Delete') }}

{{ Form::close() }}

</li>

@endforeach
</ul>

@endif

<div class="pagination">{{ $posts->render() }}</div>

<!-- {{ $posts->links() }} -->

@stop

//views/admin/post/edit.blade.php

@extends('admin._layouts.admin')

@section('content')

<h1>Edit Post</h1>

{{ Form::model($post, array('route' => array('admin.posts.update', $post->id),


'method' => 'put')) }}

@include('admin.posts._partials.form')

{{ Form::close() }}

@stop

/views/admin/post/_partials/form.blade.php

<ul>

<li>

{{ Form::label('user_id', 'Author') }}

{{ Form::select('user_id', App\User::pluck('name', 'id')) }}

{{ $errors->first('user_id', '<p class="error">:message</p>') }}

</li>

<li>

{{ Form::label('title', 'Title') }}

{{ Form::text('title') }}

{{ $errors->first('title', '<p class="error">:message</p>') }}

</li>
<li>

{{ Form::label('body', 'Body') }}

{!! Form::textarea('body') !!}

{!! $errors->first('body', '<p class="error">:message</p>') !!}

</li>

<li>

{{ Form::submit('Save') }}

</li>

</ul>

//adminpostcontroller/store

$validator = Validator::make($data = Input::all(), Post::$rules);

if ($validator->fails())

return Redirect::back()->withErrors($validator)->withInput();

Post::create($data);

return Redirect::route('admin.posts.index');

//middleware/authenticate

public function handle($request, Closure $next, $guard = null)

if (Auth::guard($guard)->guest()) {

if ($request->ajax() || $request->wantsJson()) {

return response('Unauthorized.', 401);

} else {

return redirect()->guest('admin/login');

}
return $next($request);

//routes

Route::group(['prefix' => 'admin'], function(){

Route::get('login', array('as' => 'admin.login', 'uses' =>


'Admin\AdminAuthController@getLogin'));

Route::post('login', array('as' => 'admin.login.post','uses' =>


'Admin\AdminAuthController@postLogin'));

Route::get('logout', array('as' => 'admin.logout', 'uses' =>


'Admin\AdminAuthController@getLogout'));

});

Route::group(['prefix' => 'admin','middleware' => 'auth'], function () {

Route::resource('posts', 'admin\AdminPostController', ['as' => 'admin']);

});

//admin/adminAuthcontroller

public function getLogin(){

return View('admin.auth.login');

public function postLogin(){

$data = Input::all();

$validator = Validator::make($data, User::$auth_rules);

if ($validator->fails())

return Redirect::back()->withErrors($validator)->withInput();

if (Auth::attempt(array('email' => Input::get('email'), 'password' =>


Input::get('password')))){

return Redirect::intended('admin/posts');

return Redirect::route('admin.login');

public function getLogout(){

Auth::logout();

return Redirect::route('admin.login');

/admin/auth/login.blade.php

@extends('admin._layouts.admin')

@section('content')

<h2>Please login</h2>

{{ Form::open(array('route' => 'admin.login.post')) }}

<ul>

<li>

{{ Form::label('email', 'Email') }}

{{ Form::email('email') }}

{!! $errors->first('email', '<p class="error">:message</p>') !!}

</li>

<li>

{{ Form::label('password', 'Password') }}

{{ Form::password('password') }}

{!! $errors->first('password', '<p class="error">:message</p>') !!}

</li>

<li>

{{ Form::submit('Log in') }}
</li>

</ul>

{{ Form::close() }}

@stop

//User model

protected $fillable = [

'name', 'email', 'password',

];

public static $auth_rules = [

'email' => 'required|email',

'password' => 'required'

];

//PostsController

public function getIndex(){

$posts = Post::with('user')->get();

/*dd($posts);*/

return View('posts.index', compact('posts'));

//POst model

public static $rules = [

'title' =>'required|between:3,255',

'body' => 'required',

'user_id' => 'integer'

];

protected $fillable = ['title', 'body','user_id'];


public function user(){

return $this->belongsTo('App\User');

//user model

public function posts(){

return $this->hasMany('App\Post');

//views/_layouts/default.blade.php

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

<title>My Awesome Blog</title>

{{ Html::style('css/style.css') }}

<!--[if lt IE 9]>

<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>

<![endif]-->

</head>

<body>

<header>

<div class="container">
<h1>My Blog</h1>

<p>By Joost van Veen</p>

</div>

</header>

<main class="container">

@yield('content')

</main>

<footer>

<div class="container">

&copy; {{ date('Y') }} My Awesome Company |

{{ link_to_route('admin.posts.index', 'Admin') }}

</div>

</footer>

</body>

</html>

//postscontroller

public function getpost($id){

$post = Post::with('User')->find($id);

return View('posts.post', compact('post'));

//views/posts/post.blade.php

@extends('_layouts.default')
@section('content')

<h1>{{{ $post->title }}}</h1>

<p class="created_at">Created on {{{ date('Y-m-d', strtotime($post->created_at))}}}


By {{{ $post->user->name }}}</p>

<p>{{{ $post->body }}}</p>

<p>{{ link_to_route('home', '&lsaquo; Back') }}</p>

@stop

//views/admin/_layouts/admin.blade.php

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

<script src="{{ asset('js/bootstrap.min.js') }}"></script>

<link href="{{ asset('css/bootstrap.min.css')}}" rel="stylesheet">

<script type="text/javascript"
src="{{ asset('/js/tinymce/tinymce.min.js') }}"></script>

<script>tinymce.init({

selector : "textarea",

toolbar : "insertfile undo redo | styleselect | bold italic | alignleft


aligncenter alignright alignjustify | bullist numlist outdent indent | link image
jbimages",

});</script>

<title>My Awesome Admin Panel</title>


{{ Html::style('css/admin.css') }}

<!--[if lt IE 9]>

<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script

<![endif]-->

</head>

<body>

<header>

<div class="container">

<h1>My Awesome Admin Panel</h1>

</div>

</header>

<main class="container">

@yield('content')

</main>

<footer>

<div class="container">

&copy; {{ date('Y') }} My Awesome Company

</div>

</footer>

</body>

</html>

//posts/index.blade.php

@extends('_layouts.default')
@section('content')

@foreach($posts as $post)

<article>

<h2>{{ link_to_route('post', $post->title, array($post->id)) }}</h2>

<p class="created_at">Created on {{{ date('Y-m-d', strtotime($post-


>created_at))}}} By {{{ link_to_route('post.user',$post->user->name,$post->user-
>id) }}}</p>

<p>{!! str_limit($post->body) !!}</p>

<p>{{ link_to_route('post', 'Read More &rsaquo;', array($post->id)) }}</p>

</article>

@endforeach

@stop

Route::get('/', array('as' => 'home', 'uses' => 'PostsController@getIndex'));

Route::get('post/{id}', array('as' => 'post',

'uses' => 'PostsController@getPost'))->where('id', '[1-9][0-9]*');

Route::get('post/user/{id}', array('as' => 'post.user',

'uses' => 'PostsController@getUserPost'))->where('id', '[1-9][0-9]*');

//postscontroller

public function getUserPost($id){

$users = User::with('posts')->find($id);

dd($users);

return View('posts.user', compact('users'));


}

//posts/user.blade.php

@extends('_layouts.default')

@section('content')

{{ $users->name }}

@foreach($users->posts as $post)

<article>

<h2>{{ link_to_route('post', $post->title, array($post->id)) }}</h2>

<p class="created_at">Created on {{{ date('Y-m-d', strtotime($post-


>created_at))}}} </p>

<p>{!! str_limit($post->body) !!}</p>

<p>{{ link_to_route('post', 'Read More &rsaquo;', array($post->id)) }}</p>

</article>

@endforeach

@stop

//admin/posts/create.blade.php

@extends('admin._layouts.admin')

@section('content')

<h1>Create Post</h1>

{{ Form::open(array('route' => 'admin.posts.store')) }}

@include('admin.posts._partials.form')

{{ Form::close() }}

@sto
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Http\Controllers\Controller;

use App\User;

use Illuminate\Support\Facades\Validator;

use Illuminate\Support\Facades\Input;

use Illuminate\Support\Facades\Redirect;

use Auth;

You might also like