0% found this document useful (0 votes)
99 views

Laravel Blog

This document outlines the steps to create a Laravel blog application with admin functionality for managing posts. It includes generating scaffolding for posts and seeding sample data, creating an AdminPostsController and views for listing, creating and editing posts, and adding basic styling to the admin interface. Route groups are used to namespace the admin routes under /admin.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

Laravel Blog

This document outlines the steps to create a Laravel blog application with admin functionality for managing posts. It includes generating scaffolding for posts and seeding sample data, creating an AdminPostsController and views for listing, creating and editing posts, and adding basic styling to the admin interface. Route groups are used to namespace the admin routes under /admin.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

laravel blog

create new laravel project


install laracast generator
php artisan make:scaffold
Route::resource('posts', 'PostsController', array('except' => array('show')));

class PostsTableSeeder extends Seeder {


public function run()
{
$faker = Faker::create();
foreach(range(1, 10) as $index)
{
Post::create([
'title' => $faker->sentence(),
'body' => $faker->realtext(1000),
'user_id' => rand(1, 5),
]);
}
}
}

public function run()


{
$faker = Faker::create();
foreach(range(1, 5) as $index)
{
User::create([
'email' => $faker->email(),
'name' => $faker->name(),
'password' => Hash::make('tutsplus')
]);
}
}

public function run()


{
Eloquent::unguard();
$this->call('PostsTableSeeder');
$this->call('UsersTableSeeder');
}

class Post extends \Eloquent {


// Add your validation rules here
public static $rules = [
'title' => 'required|between:3,255',
'body' => 'required',
'user_id' => 'integer',
];
// Don't forget to fill this array
protected $fillable = ['title', 'body', 'user_id'];
}

php artisan migrate --seed

copy paste all generated view files to views/admin/post

crete controllers/admin folder


<?php
class AdminPostsController extends \BaseController {
/**
* Display a listing of posts
*
* @return Response
*/
public function index()
{
$posts = Post::all();
return View::make('admin.posts.index', compact('posts'));
}
/**
* Show the form for creating a new post
*
* @return Response
*/
public function create()
{
return View::make('admin.posts.create');
}
/**
* Store a newly created post in storage.
*
* @return Response
*/
public function 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');
}
/**
* Show the form for editing the specified post.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
$post = Post::find($id);
return View::make('admin.posts.edit', compact('post'));
}
/**
* Update the specified post in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
$post = Post::findOrFail($id);
$validator = Validator::make($data = Input::all(), Post::$rules);
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
$post->update($data);
return Redirect::route('admin.posts.index');
}
/**
* Remove the specified post from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
Post::destroy($id);
return Redirect::route('admin.posts.index');
}
}

//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">
<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>

public/css/admin.css
* { font-size: 14px; font-family: helvetica, arial, sans-serif; }
body { margin: 0; padding: 0; }
h1 { font-size: 24px; }
h2 { font-size: 18px; }
h3 { font-size: 15px; }
header { padding: 20px 0; background: #eee; margin-bottom: 20px; }
main { margin-bottom: 20px; }
footer { border-top: 1px solid #ccc; margin: 40px 0; padding-top: 20px; text-align:
right; }
.container { max-width: 700px; margin: 0 auto; width: 96%; padding: 0 2%; }

/* forms */
form { width: 94%; padding: 3%; border: 1px solid #ddd; border-radius: 5px; }
form li { list-style-type: none; padding-bottom: 20px; }
form li .error { display: inline-block; color: red; }
form label { display: block; font-weight: bold; }
input[type="text"], input[type="email"], input[type="password"], textarea { border:
1px solid #ccc; border-radius: 3px; width: 96%; font-size: 14px; }
input[type="text"], input[type="email"], input[type="password"] { height: 30px;
line-height: 30px; padding: 0 2%; }
textarea { width: 96%; padding: 2%; }
input[type="submit"] { background: #4288CE; color: #fff; height: 40px; line-height:
40px; border: none; border-radius: 3px; padding: 0 20px; -webkit-transition: all
0.5s ease-in-out; -moz-transition: all 0.5s ease-in-out; -o-transition: all 0.5s
ease-in-out; transition: all 0.5s ease-in-out; }
input[type="submit"]:hover { background: #333; }
form.destroy { display: inline-block; padding: 0; border: none; width: auto; }
form.destroy input[type="submit"] { margin-left: 10px; background: #999; display:
inline-block; height: auto; padding: 0.2em; line-height: 1.2em; }
form.destroy input[type="submit"]:hover { background: red; }

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

@stop

admin/posts/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/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() }}
@stop

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

<ul>
<li>
{{ Form::label('user_id', 'Author') }}
{{ Form::select('user_id', User::lists('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>

routes/web.php

Route::group(array('prefix' => 'admin'), function(){ Route::resource('posts',


'AdminPostsController', array('except' => array('show')));
});

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy