Page 和评论将使用 Eloquent 提供的“一对多关系”。最终,我们将得到一个个人博客系统的雏形,并布置一个大作业,供大家实战练习。
1. 初识 Eloquent
Laravel Eloquent ORM 是 Laravel 中非常重要的部分,也是 Laravel 能如此流行的原因之一。中文文档在:
1. http://laravel-china.org/docs/5.0/eloquent
2. http://www.golaravel.com/laravel/docs/5.0/eloquent/
在前面的教程中已经建立好的 learnlaravel5/app/Page.php 就是一个 Eloquent Model 类:
<"codetitle">复制代码 代码如下:
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 模型:
<"http://laravel-china.org/docs/5.0/eloquent#relationships">http://laravel-china.org/docs/5.0/eloquent#relationships4. 前台提交功能
修改 Comment 模型:
<"codetitle">复制代码 代码如下:
Route::post('comment/store', 'CommentsController@store');
运行以下命令创建 CommentsController 控制器:
复制代码 代码如下:
php artisan make:controller CommentsController
修改 CommentsController:
<"htmlcode">@extends('_layouts.default') @section('content') <h4> <a href="/">"text-align: center; margin-top: 50px;">{{ $page->title }}</h1> <hr> <div id="date" style="text-align: right;"> {{ $page->updated_at }} </div> <div id="content" style="padding: 50px;"> <p> {{ $page->body }} </p> </div> <div id="comments" style="margin-bottom: 100px;"> @if (count($errors) > 0) <div class="alert alert-danger"> <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 id="new"> <form action="{{ URL('comment/store') }}" method="POST"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <input type="hidden" name="page_id" value="{{ $page->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 ($page->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> @endsection前台评论功能完成。
查看效果:
5. 后台管理功能
修改基础视图 learnlaravel5/resources/views/app.blade.php 为:
<!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>Laravel</title> <link href="/css/app.css" rel="stylesheet"> <!-- Fonts --> <link href='http://fonts.useso.com/css"navbar navbar-default"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle Navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">Learn Laravel 5</a> </div> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li><a href="/admin">后台首页</a></li> </ul> <ul class="nav navbar-nav"> <li><a href="/admin/comments">管理评论</a></li> </ul> <ul class="nav navbar-nav navbar-right"> @if (Auth::guest()) <li><a href="/auth/login">Login</a></li> <li><a href="/auth/register">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">{{ Auth::user()->name }} <span class="caret"></span></a> <ul class="dropdown-menu" role="menu"> <li><a href="/auth/logout">Logout</a></li> </ul> </li> @endif </ul> </div> </div> </nav> @yield('content') <!-- Scripts --> <script src="/UploadFiles/2021-04-02/jquery.min.js">修改后台路由组(增加了一行):
Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'auth'], function() { Route::get('/', 'AdminHomeComtroller@index'); Route::resource('pages', 'PagesController'); Route::resource('comments', 'CommentsController'); });创建 Admin\CommentsController :
复制代码 代码如下:
php artisan make:controller Admin/CommentsController
Admin/CommentsController 要有 查看所有、查看单个、POST更改、删除四个接口:
<"htmlcode">@extends('app') @section('content') <div class="container"> <div class="row"> <div class="col-md-10 col-md-offset-1"> <div class="panel panel-default"> <div class="panel-heading">管理评论</div> <div class="panel-body"> <table class="table table-striped"> <tr class="row"> <th class="col-lg-4">Content</th> <th class="col-lg-2">User</th> <th class="col-lg-4">Page</th> <th class="col-lg-1">编辑</th> <th class="col-lg-1">删除</th> </tr> @foreach ($comments as $comment) <tr class="row"> <td class="col-lg-6"> {{ $comment->content }} </td> <td class="col-lg-2"> @if ($comment->website) <a href="{{ $comment->website }}"> <h4>{{ $comment->nickname }}</h4> </a> @else <h3>{{ $comment->nickname }}</h3> @endif {{ $comment->email }} </td> <td class="col-lg-4"> <a href="{{ URL('pages/'.$comment->page_id) }}" target="_blank"> {{ App\Page::find($comment->page_id)->title }} </a> </td> <td class="col-lg-1"> <a href="{{ URL('admin/comments/'.$comment->id.'/edit') }}" class="btn btn-success">编辑</a> </td> <td class="col-lg-1"> <form action="{{ URL('admin/comments/'.$comment->id) }}" method="POST" style="display: inline;"> <input name="_method" type="hidden" value="DELETE"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <button type="submit" class="btn btn-danger">删除</button> </form> </td> </tr> @endforeach </table> </div> </div> </div> </div> </div> @endsectionlearnlaravel5/resources/views/admin/comments/edit.blade.php:
@extends('app') @section('content') <div class="container"> <div class="row"> <div class="col-md-10 col-md-offset-1"> <div class="panel panel-default"> <div class="panel-heading">编辑评论</div> <div class="panel-body"> @if (count($errors) > 0) <div class="alert alert-danger"> <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 <form action="{{ URL('admin/comments/'.$comment->id) }}" method="POST"> <input name="_method" type="hidden" value="PUT"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <input type="hidden" name="page_id" value="{{ $comment->page_id }}"> Nickname: <input type="text" name="nickname" class="form-control" required="required" value="{{ $comment->nickname }}"> <br> Email: <input type="text" name="email" class="form-control" required="required" value="{{ $comment->email }}"> <br> Website: <input type="text" name="website" class="form-control" required="required" value="{{ $comment->website }}"> <br> Content: <textarea name="content" rows="10" class="form-control" required="required">{{ $comment->content }}</textarea> <br> <button class="btn btn-lg btn-info">提交修改</button> </form> </div> </div> </div> </div> </div> @endsection后台管理功能完成,查看效果:
6. 大作业
依赖于 Page 的评论功能已经全部完成,个人博客系统雏形诞生。在本系列教程的最后,布置一个大作业:构建出 Article 的前后台,并且加上 Article 与 Comment 的一对多关系,加入评论和评论管理功能。在做这个大作业的过程中,你将会反复地回头去看前面的教程,反复地阅读中文文档,会仔细阅读我的代码,等你完成大作业的时候,Laravel 5 就真正入门啦~~以上所述就是本文的全部内容了,希望大家能够喜欢。
华山资源网 Design By www.eoogi.com广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!华山资源网 Design By www.eoogi.com暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 张敬轩2005《我的梦想我的路》几何娱乐[WAV+CUE][1G]
- 群星《人到四十男儿情(SRS+WIZOR)》[原抓WAV+CUE]
- 马久越《上善若水HQCDII》[低速原抓WAV+CUE]
- 龚玥《女儿情思》6N纯银SQCD【WAV+CUE】
- 张惠妹《你在看我吗》大碟15 金牌大风[WAV+CUE][1G]
- 群星《左耳·听见爱情》星文唱片[WAV+CUE][1G]
- 群星《抖音嗨疯-DISCO英文版》[WAV+CUE][1G]
- 群星.1990-情义无价(TP版)【中唱】【WAV+CUE】
- 马兆骏.1990-心情·七月【滚石】【WAV+CUE】
- 方伊琪.1979-沙鸥(LP版)【星岛全音】【WAV+CUE】
- 蔡琴《醇厚嗓音》6N纯银SQCD【WAV+CUE】
- 陈曦《遇见HQCD》[WAV+CUE]
- 大提琴-刘欣欣《爱的问候》HDCD[WAV+CUE]
- 周耀辉/邓慧中《从什么时候开始》[320K/MP3][95.71MB]
- 周耀辉/邓慧中《从什么时候开始》[FLAC/分轨][361.29MB]