基礎規則
- 需要新建一個表專門用來存放每一條評論,每一條評論都屬於某一篇文章。
建立 model 和資料表
- 建立名為Comment的model,並順便建立附帶的migration
php artisan make:model Comment -m
- 這樣一次性的建立兩個文件
database/migrations/2017_11_11_151823_create_comments_table.php
和Comment類
- 填寫
up()
方法,給comments
表增加
1 2 3 4 5 6 7 8 9 10 11 12
| public function up() { Schema::create('comments', function (Blueprint $table) { $table->increments('id'); $table->string('nickname'); $table->string('email')->nullable(); $table->string('website')->nullable(); $table->text('content')->nullable(); $table->integer('article_id'); $table->timestamps(); }); }
|
- 接著運行
php artisan migrate
- 最後資料表已經建立完成
建立一對多關係
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model { public function hasManyComments() { return $this->hasMany('App\Comment', 'article_id', 'id'); } }
|
構建前台UI
建立前台的 ArticleController
php artisan make:controller ArticleController
Route::get('article/{id}', 'ArticleController@show');
1 2 3 4
| public function show($id) { return view('article/show')->withArticle(Article::with('hasManyComments')->find($id)); }
|
1 2 3 4 5 6 7
| .... use App\Article;
class ArticleController extends Controller { .... }
|
建立前台文章
- 建立
resources/views/article/show.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>Learn Laravel 5</title>
<link href="//cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script> <script src="//cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head>
<div id="content" style="padding: 50px;">
<h4> <a href="/"><< 返回首页</a> </h4>
<h1 style="text-align: center; margin-top: 50px;">{{ $article->title }}</h1> <hr> <div id="date" style="text-align: right;"> {{ $article->updated_at }} </div> <div id="content" style="margin: 20px;"> <p> {{ $article->body }} </p> </div>
<div id="comments" style="margin-top: 50px;">
@if (count($errors) > 0) <div class="alert alert-danger"> <strong>操作失败</strong> 输入不符合要求<br><br> {!! implode('<br>', $errors->all()) !!} </div> @endif
<div id="new"> <form action="{{ url('comment') }}" method="POST"> {!! csrf_field() !!} <input type="hidden" name="article_id" value="{{ $article->id }}"> <div class="form-group"> <label>Nickname</label> <input type="text" name="nickname" class="form-control" style="width: 300px;" required="required"> </div> <div class="form-group"> <label>Email address</label> <input type="email" name="email" class="form-control" style="width: 300px;"> </div> <div class="form-group"> <label>Home page</label> <input type="text" name="website" class="form-control" style="width: 300px;"> </div> <div class="form-group"> <label>Content</label> <textarea name="content" id="newFormContent" class="form-control" rows="10" required="required"></textarea> </div> <button type="submit" class="btn btn-lg btn-success col-lg-12">Submit</button> </form> </div>
<script> function reply(a) { var nickname = a.parentNode.parentNode.firstChild.nextSibling.getAttribute('data'); var textArea = document.getElementById('newFormContent'); textArea.innerHTML = '@'+nickname+' '; } </script>
<div class="conmments" style="margin-top: 100px;"> @foreach ($article->hasManyComments as $comment)
<div class="one" style="border-top: solid 20px #efefef; padding: 5px 20px;"> <div class="nickname" data="{{ $comment->nickname }}"> @if ($comment->website) <a href="{{ $comment->website }}"> <h3>{{ $comment->nickname }}</h3> </a> @else <h3>{{ $comment->nickname }}</h3> @endif <h6>{{ $comment->created_at }}</h6> </div> <div class="content"> <p style="padding: 20px;"> {{ $comment->content }} </p> </div> <div class="reply" style="text-align: right; padding: 5px;"> <a href="#new" onclick="reply(this);">回复</a> </div> </div>
@endforeach </div> </div>
</div>
</body> </html>
|
建立評論儲存功能
- 建立controller
php artisan make:controller CommentController
- 建立route
Route::post('comment', 'CommentController@store');
- 增加 store 函數
1 2 3 4 5 6 7 8
| public function store(Request $request) { if (Comment::create($request->all())) { return redirect()->back(); } else { return redirect()->back()->withInput()->withErrors('评论发表失败!'); } }
|
- 批量賦值
採用批量賦值方法來減少儲存評論的代碼
model可利用「create」方法將array型態的資料進行模型(實體)的新增,而一般的「save」方法僅能進行單一屬性(欄位)設定,例如:
1 2 3 4 5 6 7 8 9 10 11 12
| $data = [ 'column_1' => '...', 'column_2' => '...', ]; Post::create($data);
$post = new Post; $post->column_1 = '...'; $post->column_2 = '...'; $post->save();
|