{{ $page->body }}
摘要:原文發(fā)表在我的個人網(wǎng)站系列入門教程四最適合中國人的教程本教程示例代碼見大家在任何地方卡住,最快捷的解決方式就是去看我的示例代碼。
原文發(fā)表在我的個人網(wǎng)站:Laravel 5 系列入門教程(四)【最適合中國人的 Laravel 教程】
本教程示例代碼見:https://github.com/johnlui/Learn-Laravel-5大家在任何地方卡住,最快捷的解決方式就是去看我的示例代碼。
本文是本系列教程的完結(jié)篇,我們將一起給 Page 加入評論功能,讓游客在前臺頁面可以查看、提交、回復(fù)評論,同時我們將在后臺完善評論管理功能,可以刪除、編輯評論。Page 和評論將使用 Eloquent 提供的“一對多關(guān)系”。最終,我們將得到一個個人博客系統(tǒng)的雛形,并布置一個大作業(yè),供大家實(shí)戰(zhàn)練習(xí)。
1. 初識 EloquentLaravel Eloquent ORM 是 Laravel 中非常重要的部分,也是 Laravel 能如此流行的原因之一。中文文檔在:
http://laravel-china.org/docs/5.0/eloquent
http://www.golaravel.com/laravel/docs/5.0/eloquent/
在前面的教程中已經(jīng)建立好的 learnlaravel5/app/Page.php 就是一個 Eloquent Model 類:
php若想進(jìn)一步了解 Eloquent,推薦閱讀系列文章:深入理解 Laravel Eloquent。
2. 創(chuàng)建 Comment 模型首先我們要新建一張表來存儲 Comment,命令行運(yùn)行:
bashphp artisan make:model Comment成功以后,修改 migration 文件 learnlaravel5/database/migrations/***_create_comments_table.php 的相應(yīng)位置為:
phpSchema::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(); });之后運(yùn)行:
bashphp artisan migrate去數(shù)據(jù)庫里瞧瞧,comments 表已經(jīng)躺在那兒啦。
3. 建立“一對多關(guān)系”修改 Page 模型:
phphasMany("AppComment", "page_id", "id"); } }搞定啦~ Eloquent 中模型間關(guān)系就是這么簡單。
模型間關(guān)系中文文檔:http://laravel-china.org/docs/5.0/eloquent#relationships
擴(kuò)展閱讀:深入理解 Laravel Eloquent(三)——模型間關(guān)系(關(guān)聯(lián))
4. 前臺提交功能修改 Comment 模型:
php增加一行路由:
phpRoute::post("comment/store", "CommentsController@store");運(yùn)行以下命令創(chuàng)建 CommentsController 控制器:
phpphp artisan make:controller CommentsController修改 CommentsController:
phpwithInput()->withErrors("評論發(fā)表失敗!"); } } }修改視圖 learnlaravel5/resources/views/pages/show.blade.php:
php@extends("_layouts.default") @section("content")??返回首頁
{{ $page->title }}
{{ $page->updated_at }}{{ $page->body }}
@if (count($errors) > 0)@endsectionWhoops! There were some problems with your input.@endif
@foreach ($errors->all() as $error)
- {{ $error }}
@endforeach@foreach ($page->hasManyComments as $comment)@endforeach@if ($comment->website){{ $comment->nickname }}
@else{{ $comment->nickname }}
@endif{{ $comment->created_at }}
{{ $comment->content }}
前臺評論功能完成。
查看效果:
5. 后臺管理功能修改基礎(chǔ)視圖 learnlaravel5/resources/views/app.blade.php 為:
phpLaravel @yield("content")修改后臺路由組(增加了一行):
phpRoute::group(["prefix" => "admin", "namespace" => "Admin", "middleware" => "auth"], function() { Route::get("/", "AdminHomeComtroller@index"); Route::resource("pages", "PagesController"); Route::resource("comments", "CommentsController"); });創(chuàng)建 AdminCommentsController :
bashphp artisan make:controller Admin/CommentsControllerAdmin/CommentsController 要有 查看所有、查看單個、POST更改、刪除四個接口:
phpwithComments(Comment::all()); } public function edit($id) { return view("admin.comments.edit")->withComment(Comment::find($id)); } public function update(Request $request, $id) { $this->validate($request, [ "nickname" => "required", "content" => "required", ]); if (Comment::where("id", $id)->update(Input::except(["_method", "_token"]))) { return Redirect::to("admin/comments"); } else { return Redirect::back()->withInput()->withErrors("更新失?。?); } } public function destroy($id) { $comment = Comment::find($id); $comment->delete(); return Redirect::to("admin/comments"); } }接下來創(chuàng)建兩個視圖:
learnlaravel5/resources/views/admin/comments/index.blade.php:
php@extends("app") @section("content")@endsection管理評論
@foreach ($comments as $comment) Content User Page 編輯 刪除 @endforeach {{ $comment->content }} @if ($comment->website) {{ $comment->nickname }}
@else{{ $comment->nickname }}
@endif {{ $comment->email }}page_id) }}" target="_blank"> {{ AppPage::find($comment->page_id)->title }} id."/edit") }}" class="btn btn-success">編輯 learnlaravel5/resources/views/admin/comments/edit.blade.php:
php@extends("app") @section("content")@endsection編輯評論@if (count($errors) > 0)Whoops! There were some problems with your input.@endif
@foreach ($errors->all() as $error)
- {{ $error }}
@endforeach后臺管理功能完成,查看效果:
6. 大作業(yè)依賴于 Page 的評論功能已經(jīng)全部完成,個人博客系統(tǒng)雛形誕生。在本系列教程的最后,布置一個大作業(yè):構(gòu)建出 Article 的前后臺,并且加上 Article 與 Comment 的一對多關(guān)系,加入評論和評論管理功能。在做這個大作業(yè)的過程中,你將會反復(fù)地回頭去看前面的教程,反復(fù)地閱讀中文文檔,會仔細(xì)閱讀我的代碼,等你完成大作業(yè)的時候,Laravel 5 就真正入門啦~~
教程(四)代碼快照:https://github.com/johnlui/Learn-Laravel-5/archive/tutorial_4.zip
Laravel 5 系列入門教程【最適合中國人的 Laravel 教程】到此結(jié)束,謝謝大家!
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/30208.html
摘要:完結(jié)教程一教程二教程三原文是富文本編輯器寫的,向不好轉(zhuǎn)換,大家點(diǎn)過去看吧。。。 【完結(jié)】 教程(一):http://lvwenhan.com/laravel/398.html 教程(二): http://lvwenhan.com/laravel/399.html 教程(三): http://lvwenhan.com/laravel/400.html 原文是富文本編輯器寫的...
摘要:原文發(fā)表在我的個人網(wǎng)站系列入門教程一最適合中國人的教程本教程示例代碼見大家在任何地方卡住,最快捷的解決方式就是去看我的示例代碼。在此我推薦一個全量中國鏡像。 原文發(fā)表在我的個人網(wǎng)站:Laravel 5 系列入門教程(一)【最適合中國人的 Laravel 教程】 本教程示例代碼見:https://github.com/johnlui/Learn-Laravel-5 大...
摘要:原文發(fā)表在我的個人網(wǎng)站系列入門教程三最適合中國人的教程本教程示例代碼見大家在任何地方卡住,最快捷的解決方式就是去看我的示例代碼。教程三代碼快照下一步系列入門教程四最適合中國人的教程完結(jié) 原文發(fā)表在我的個人網(wǎng)站:Laravel 5 系列入門教程(三)【最適合中國人的 Laravel 教程】 本教程示例代碼見:https://github.com/johnlui/Learn-L...
摘要:原文發(fā)表在我的個人網(wǎng)站系列入門教程二最適合中國人的教程本教程示例代碼見大家在任何地方卡住,最快捷的解決方式就是去看我的示例代碼。 原文發(fā)表在我的個人網(wǎng)站:Laravel 5 系列入門教程(二)【最適合中國人的 Laravel 教程】 本教程示例代碼見:https://github.com/johnlui/Learn-Laravel-5 大家在任何地方卡住,最快捷...
閱讀 2617·2021-10-14 09:43
閱讀 3574·2021-10-13 09:39
閱讀 3309·2019-08-30 15:44
閱讀 3157·2019-08-29 16:37
閱讀 3722·2019-08-29 13:17
閱讀 2747·2019-08-26 13:57
閱讀 1841·2019-08-26 11:59
閱讀 1267·2019-08-26 11:46