Page 和评论将使用 Eloquent 提供的“一对多关系”。最终,我们将得到一个个人博客系统的雏形,并布置一个大作业,供大家实战练习。
1. 初识 Eloquent
Laravel Eloquent ORM 是 Laravel 中非常重要的部分,也是 Laravel 能如此流行的原因之一。中文文档在:
在前面的教程中已经建立好的 learnlaravel5/app/Page.php 就是一个 Eloquent Model 类:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Page extends Model { // }
若想进一步了解 Eloquent,推荐阅读系列文章:深入理解 Laravel Eloquent
2. 创建 Comment 模型
首先我们要新建一张表来存储 Comment,命令行运行:
复制代码 代码如下:
php artisan make:model Comment
成功以后,修改 migration 文件 learnlaravel5/database/migrations/***_create_comments_table.php 的相应位置为:
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('page_id'); $table->timestamps(); });
之后运行:
复制代码 代码如下:
php artisan migrate
去数据库里瞧瞧,comments 表已经躺在那儿啦。
3. 建立“一对多关系”
修改 Page 模型:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Page extends Model { public function hasManyComments() { return $this->hasMany('App\Comment', 'page_id', 'id'); } }
搞定啦~ Eloquent 中模型间关系就是这么简单。
模型间关系中文文档:
4. 前台提交功能
修改 Comment 模型:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Comment extends Model { protected $fillable = ['nickname', 'email', 'website', 'content', 'page_id']; }
增加一行路由:
复制代码 代码如下:
Route::post('comment/store', 'CommentsController@store');
运行以下命令创建 CommentsController 控制器:
复制代码 代码如下:
php artisan make:controller CommentsController
修改 CommentsController:
<?php namespace App\Http\Controllers; use App\Http\Requests; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Redirect, Input; use App\Comment; class CommentsController extends Controller { public function store() { if (Comment::create(Input::all())) { return Redirect::back(); } else { return Redirect::back()->withInput()->withErrors('评论发表失败!'); } } }
修改视图 learnlaravel5/resources/views/pages/show.blade.php:
@extends('_layouts.default') @section('content') <h4> <a href="https://www.jb51.net/">⬅️返回首页</a> </h4> <h1>{{ $page->title }}</h1> <hr> <div> {{ $page->updated_at }} </div> <div> <p> {{ $page->body }} </p> </div> <div> @if (count($errors) > 0) <div> <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <div> <form action="{{ URL('comment/store') }}" method="POST"> <input type="hidden" value="{{ csrf_token() }}"> <input type="hidden" value="{{ $page->id }}"> <div> <label>Nickname</label> <input type="text" required="required"> </div> <div> <label>Email address</label> <input type="email"> </div> <div> <label>Home page</label> <input type="text"> </div> <div> <label>Content</label> <textarea rows="10" required="required"></textarea> </div> <button type="submit">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> @foreach ($page->hasManyComments as $comment) <div> <div 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> <p> {{ $comment->content }} </p> </div> <div> <a href="#new">回复</a> </div> </div> @endforeach </div> </div> @endsection
前台评论功能完成。
查看效果:
5. 后台管理功能
修改基础视图 learnlaravel5/resources/views/app.blade.php 为: