Skip to content

Commit 1cba6cc

Browse files
committed
教程四完成,系列教程结束。
1 parent ae92c17 commit 1cba6cc

File tree

12 files changed

+309
-13
lines changed

12 files changed

+309
-13
lines changed

app/Comment.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php namespace App;
2+
3+
use Illuminate\Database\Eloquent\Model;
4+
5+
class Comment extends Model {
6+
7+
protected $fillable = ['nickname', 'email', 'website', 'content', 'page_id'];
8+
9+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php namespace App\Http\Controllers\Admin;
2+
3+
use App\Http\Requests;
4+
use App\Http\Controllers\Controller;
5+
6+
use Illuminate\Http\Request;
7+
8+
use App\Comment;
9+
10+
use Redirect, Input;
11+
12+
class CommentsController extends Controller {
13+
14+
public function index()
15+
{
16+
return view('admin.comments.index')->withComments(Comment::all());
17+
}
18+
19+
public function edit($id)
20+
{
21+
return view('admin.comments.edit')->withComment(Comment::find($id));
22+
}
23+
24+
public function update(Request $request, $id)
25+
{
26+
$this->validate($request, [
27+
'nickname' => 'required',
28+
'content' => 'required',
29+
]);
30+
if (Comment::where('id', $id)->update(Input::except(['_method', '_token']))) {
31+
return Redirect::to('admin/comments');
32+
} else {
33+
return Redirect::back()->withInput()->withErrors('更新失败!');
34+
}
35+
}
36+
37+
public function destroy($id)
38+
{
39+
$comment = Comment::find($id);
40+
$comment->delete();
41+
42+
return Redirect::to('admin/comments');
43+
}
44+
45+
}

app/Http/Controllers/Admin/PagesController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function store(Request $request)
3636
$page = new Page;
3737
$page->title = Input::get('title');
3838
$page->body = Input::get('body');
39-
$page->user_id = 1;//Auth::user()->id;
39+
$page->user_id = Auth::user()->id;
4040

4141
if ($page->save()) {
4242
return Redirect::to('admin');
@@ -73,7 +73,7 @@ public function update(Request $request,$id)
7373
$page = Page::find($id);
7474
$page->title = Input::get('title');
7575
$page->body = Input::get('body');
76-
$page->user_id = 1;//Auth::user()->id;
76+
$page->user_id = Auth::user()->id;
7777

7878
if ($page->save()) {
7979
return Redirect::to('admin');

app/Http/Controllers/Auth/AuthController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class AuthController extends Controller {
2020

2121
use AuthenticatesAndRegistersUsers;
2222

23+
public $redirectPath = '/admin';
24+
2325
/**
2426
* Create a new authentication controller instance.
2527
*
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php namespace App\Http\Controllers;
2+
3+
use App\Http\Requests;
4+
use App\Http\Controllers\Controller;
5+
6+
use Illuminate\Http\Request;
7+
8+
use Redirect, Input;
9+
10+
use App\Comment;
11+
12+
class CommentsController extends Controller {
13+
14+
public function store()
15+
{
16+
if (Comment::create(Input::all())) {
17+
return Redirect::back();
18+
} else {
19+
return Redirect::back()->withInput()->withErrors('评论发表失败!');
20+
}
21+
22+
}
23+
24+
}

app/Http/routes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
Route::get('/', 'HomeController@index');
1515

1616
Route::get('pages/{id}', 'PagesController@show');
17+
Route::post('comment/store', 'CommentsController@store');
1718

1819
Route::get('auth/login', 'Auth\AuthController@getLogin');
1920
Route::post('auth/login', 'Auth\AuthController@postLogin');
@@ -23,4 +24,5 @@
2324
{
2425
Route::get('/', 'AdminHomeComtroller@index');
2526
Route::resource('pages', 'PagesController');
27+
Route::resource('comments', 'CommentsController');
2628
});

app/Page.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
class Page extends Model {
66

7-
//
7+
public function hasManyComments()
8+
{
9+
return $this->hasMany('App\Comment', 'page_id', 'id');
10+
}
811

912
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreateCommentsTable extends Migration {
7+
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::create('comments', function(Blueprint $table)
16+
{
17+
$table->increments('id');
18+
$table->string('nickname');
19+
$table->string('email')->nullable();
20+
$table->string('website')->nullable();
21+
$table->text('content')->nullable();
22+
$table->integer('page_id');
23+
$table->timestamps();
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::drop('comments');
35+
}
36+
37+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
@extends('app')
2+
3+
@section('content')
4+
<div class="container">
5+
<div class="row">
6+
<div class="col-md-10 col-md-offset-1">
7+
<div class="panel panel-default">
8+
<div class="panel-heading">编辑评论</div>
9+
10+
<div class="panel-body">
11+
12+
@if (count($errors) > 0)
13+
<div class="alert alert-danger">
14+
<strong>Whoops!</strong> There were some problems with your input.<br><br>
15+
<ul>
16+
@foreach ($errors->all() as $error)
17+
<li>{{ $error }}</li>
18+
@endforeach
19+
</ul>
20+
</div>
21+
@endif
22+
23+
<form action="{{ URL('admin/comments/'.$comment->id) }}" method="POST">
24+
<input name="_method" type="hidden" value="PUT">
25+
<input type="hidden" name="_token" value="{{ csrf_token() }}">
26+
<input type="hidden" name="page_id" value="{{ $comment->page_id }}">
27+
Nickname: <input type="text" name="nickname" class="form-control" required="required" value="{{ $comment->nickname }}">
28+
<br>
29+
Email:
30+
<input type="text" name="email" class="form-control" required="required" value="{{ $comment->email }}">
31+
<br>
32+
Website:
33+
<input type="text" name="website" class="form-control" required="required" value="{{ $comment->website }}">
34+
<br>
35+
Content:
36+
<textarea name="content" rows="10" class="form-control" required="required">{{ $comment->content }}</textarea>
37+
<br>
38+
<button class="btn btn-lg btn-info">提交修改</button>
39+
</form>
40+
41+
</div>
42+
</div>
43+
</div>
44+
</div>
45+
</div>
46+
@endsection
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
@extends('app')
2+
3+
@section('content')
4+
<div class="container">
5+
<div class="row">
6+
<div class="col-md-10 col-md-offset-1">
7+
<div class="panel panel-default">
8+
<div class="panel-heading">管理评论</div>
9+
10+
<div class="panel-body">
11+
12+
<table class="table table-striped">
13+
<tr class="row">
14+
<th class="col-lg-4">Content</th>
15+
<th class="col-lg-2">User</th>
16+
<th class="col-lg-4">Page</th>
17+
<th class="col-lg-1">编辑</th>
18+
<th class="col-lg-1">删除</th>
19+
</tr>
20+
@foreach ($comments as $comment)
21+
<tr class="row">
22+
<td class="col-lg-6">
23+
{{ $comment->content }}
24+
</td>
25+
<td class="col-lg-2">
26+
@if ($comment->website)
27+
<a href="{{ $comment->website }}">
28+
<h4>{{ $comment->nickname }}</h4>
29+
</a>
30+
@else
31+
<h3>{{ $comment->nickname }}</h3>
32+
@endif
33+
{{ $comment->email }}
34+
</td>
35+
<td class="col-lg-4">
36+
<a href="{{ URL('pages/'.$comment->page_id) }}" target="_blank">
37+
{{ App\Page::find($comment->page_id)->title }}
38+
</a>
39+
</td>
40+
<td class="col-lg-1">
41+
<a href="{{ URL('admin/comments/'.$comment->id.'/edit') }}" class="btn btn-success">编辑</a>
42+
</td>
43+
<td class="col-lg-1">
44+
<form action="{{ URL('admin/comments/'.$comment->id) }}" method="POST" style="display: inline;">
45+
<input name="_method" type="hidden" value="DELETE">
46+
<input type="hidden" name="_token" value="{{ csrf_token() }}">
47+
<button type="submit" class="btn btn-danger">删除</button>
48+
</form>
49+
</td>
50+
</tr>
51+
@endforeach
52+
</table>
53+
54+
55+
</div>
56+
</div>
57+
</div>
58+
</div>
59+
</div>
60+
@endsection

resources/views/app.blade.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,7 @@
99
<link href="/css/app.css" rel="stylesheet">
1010

1111
<!-- Fonts -->
12-
<link href='//fonts.googleapis.com/css?family=Roboto:400,300' rel='stylesheet' type='text/css'>
13-
14-
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
15-
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
16-
<!--[if lt IE 9]>
17-
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
18-
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
19-
<![endif]-->
12+
<link href='http://fonts.useso.com/css?family=Roboto:400,300' rel='stylesheet' type='text/css'>
2013
</head>
2114
<body>
2215
<nav class="navbar navbar-default">
@@ -28,12 +21,15 @@
2821
<span class="icon-bar"></span>
2922
<span class="icon-bar"></span>
3023
</button>
31-
<a class="navbar-brand" href="#">Laravel</a>
24+
<a class="navbar-brand" href="#">Learn Laravel 5</a>
3225
</div>
3326

3427
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
3528
<ul class="nav navbar-nav">
36-
<li><a href="/">Home</a></li>
29+
<li><a href="/admin">后台首页</a></li>
30+
</ul>
31+
<ul class="nav navbar-nav">
32+
<li><a href="/admin/comments">管理评论</a></li>
3733
</ul>
3834

3935
<ul class="nav navbar-nav navbar-right">

resources/views/pages/show.blade.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,76 @@
1515
{{ $page->body }}
1616
</p>
1717
</div>
18+
<div id="comments" style="margin-bottom: 100px;">
19+
20+
@if (count($errors) > 0)
21+
<div class="alert alert-danger">
22+
<strong>Whoops!</strong> There were some problems with your input.<br><br>
23+
<ul>
24+
@foreach ($errors->all() as $error)
25+
<li>{{ $error }}</li>
26+
@endforeach
27+
</ul>
28+
</div>
29+
@endif
30+
31+
<div id="new">
32+
<form action="{{ URL('comment/store') }}" method="POST">
33+
<input type="hidden" name="_token" value="{{ csrf_token() }}">
34+
<input type="hidden" name="page_id" value="{{ $page->id }}">
35+
<div class="form-group">
36+
<label>Nickname</label>
37+
<input type="text" name="nickname" class="form-control" style="width: 300px;" required="required">
38+
</div>
39+
<div class="form-group">
40+
<label>Email address</label>
41+
<input type="email" name="email" class="form-control" style="width: 300px;">
42+
</div>
43+
<div class="form-group">
44+
<label>Home page</label>
45+
<input type="text" name="website" class="form-control" style="width: 300px;">
46+
</div>
47+
<div class="form-group">
48+
<label>Content</label>
49+
<textarea name="content" id="newFormContent" class="form-control" rows="10" required="required"></textarea>
50+
</div>
51+
<button type="submit" class="btn btn-lg btn-success col-lg-12">Submit</button>
52+
</form>
53+
</div>
54+
55+
<script>
56+
function reply(a) {
57+
var nickname = a.parentNode.parentNode.firstChild.nextSibling.getAttribute('data');
58+
var textArea = document.getElementById('newFormContent');
59+
textArea.innerHTML = '@'+nickname+' ';
60+
}
61+
</script>
62+
63+
<div class="conmments" style="margin-top: 100px;">
64+
@foreach ($page->hasManyComments as $comment)
65+
66+
<div class="one" style="border-top: solid 20px #efefef; padding: 5px 20px;">
67+
<div class="nickname" data="{{ $comment->nickname }}">
68+
@if ($comment->website)
69+
<a href="{{ $comment->website }}">
70+
<h3>{{ $comment->nickname }}</h3>
71+
</a>
72+
@else
73+
<h3>{{ $comment->nickname }}</h3>
74+
@endif
75+
<h6>{{ $comment->created_at }}</h6>
76+
</div>
77+
<div class="content">
78+
<p style="padding: 20px;">
79+
{{ $comment->content }}
80+
</p>
81+
</div>
82+
<div class="reply" style="text-align: right; padding: 5px;">
83+
<a href="#new" onclick="reply(this);">回复</a>
84+
</div>
85+
</div>
86+
87+
@endforeach
88+
</div>
89+
</div>
1890
@endsection

0 commit comments

Comments
 (0)