Laravel 5 Framework Primer (iv) End of chapter, Laravel end
Page and comment will use the "one-to-many relationship" provided by eloquent. Finally, we will get a prototype of a personal blog system, and decorate a big job for everyone to practice.
1. Initial knowledge of eloquent
Laravel eloquent ORM is a very important part of Laravel and one of the reasons why Laravel can be so popular. Chinese documents in:
1. Http://laravel-china.org/docs/5.0/eloquent
2. http://www.golaravel.com/laravel/docs/5.0/eloquent/
The learnlaravel5/app/page.php that have been established in the previous tutorials is a eloquent Model class:
<?php namespace App;use illuminate\database\eloquent\model;class Page extends Model {//}
To learn more about eloquent, we recommend reading a series of articles: in-depth understanding of Laravel eloquent
2. Create a Comment model
First we will create a new table to store the Comment, the command line runs:
Copy the Code code as follows:
PHP Artisan Make:model Comment
After success, the corresponding location for modifying the migration file learnlaravel5/database/migrations/***_create_comments_table.php is:
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 ();});
Then run:
Copy the Code code as follows:
PHP Artisan Migrate
Go to the database and see, the comments table is already lying there.
3. Establish a "one-to-many relationship"
To modify the Page model:
<?php namespace App;use illuminate\database\eloquent\model;class Page extends Model {public Function hasmanycomments () { return $this->hasmany (' app\comment ', ' page_id ', ' id ');}}
That's it. The relationship between models in eloquent is so simple.
Relationship between models Chinese document: Http://laravel-china.org/docs/5.0/eloquent#relationships
4. Foreground submission function
To modify the Comment model:
<?php namespace App;use illuminate\database\eloquent\model;class Comment extends Model {protected $fillable = [' Nickn Ame ', ' email ', ' website ', ' content ', ' page_id '];}
Add a journey by:
Copy the Code code as follows:
Route::p ost (' Comment/store ', ' commentscontroller@store ');
Run the following command to create the Commentscontroller controller:
Copy the Code code as follows:
PHP Artisan Make:controller Commentscontroller
Modify 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 () {i F (Comment::create (Input::all ())) { return redirect::back ();} else { return redirect::back ()->withinput () ->witherrors (' Comment publication failed! '); } }}
To modify the view learnlaravel5/resources/views/pages/show.blade.php:
{{$page->title}}
{{$page->updated_at}} {{$page->body}}
@if (Count ($errors) > 0) whoops! There were some problems with your input.
@foreach ($errors->all () as $error)
- {{$error}}
@endforeach
@endif @foreach ($page->hasmanycomments as $comment) nickname}} "> @if ($comment->website) website}} " >
{{$comment->nickname}}
@else
{{$comment->nickname}}
@endif
{{$comment->created_at}}
{{$comment->content}}
Reply to @endforeach @endsection
The front desk comment function is complete.
To see the effect:
5. Background management function
Modify the base view learnlaravel5/resources/views/app.blade.php to:
<title>Laravel</title>
Toggle Navigation Learn Laravel 5
@if (Auth::guest ())
- Login
- Register
@else
- {{Auth::user ()->name}}
@endif
@yield (' content ')
Modify a background routing group (adds a row):
Route::group ([' prefix ' = ' admin ', ' namespace ' = ' admin ', ' middleware ' = ' auth '], function () {route::get ('/'), ' Adminhomecomtroller@index '); Route::resource (' pages ', ' Pagescontroller '); Route::resource (' comments ', ' Commentscontroller ');});
Create Admin\commentscontroller:
Copy the Code code as follows:
PHP Artisan Make:controller Admin/commentscontroller
Admin/commentscontroller to have View all, view single, post changes, delete four interfaces:
<?php namespace App\http\controllers\admin;use app\http\requests;use app\http\controllers\controller;use Illuminate\http\request;use app\comment;use Redirect, Input;class Commentscontroller extends Controller {public function index () {return view (' Admin.comments.index ')->withcomments (Comment::all ());} Public function edit ($id) { Return view (' Admin.comments.edit ')->withcomment (Comment::find ($id)); The 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 (' Update failed! '); }} Public function Destroy ($id) {$comment = Comment::find ($id); $comment->delete (); return redirect::to (' Admin/comme NTS '); }}
Next, create two views:
learnlaravel5/resources/views/admin/comments/index.blade.php:
@extends (' app ') @section (' content ') management reviews
content |
user |
page |
edit |
delete |
@foreach ($comments as $comment)
{{$comment->content}} |
@if ($comment->website) website}" > {{$comment->nickname}} @else {{$comment->nickname}} @endif {{$comment->email}} |
page_id)}} "target=" _blank "> {{app\page::find ($comment->page_id)->title}} |
ID. ' /edit ')}} "class=" Btn btn-success "> Edit |
|
@endforeach
@endsection
learnlaravel5/resources/views/admin/comments/edit.blade.php:
@extends (' app ') @section (' content ') editorial comments @if (count ($errors) > 0) whoops! There were some problems with your input.
@foreach ($errors->all () as $error)
- {{$error}}
@endforeach
@endif @endsection
The background management function is completed to see the effect:
6. Big Job
Page-dependent commenting has been fully completed and a prototype of the personal blogging system was born. At the end of this series of tutorials, lay out a big job: build the front and back of the article, plus a one-to-many relationship with article and Comment, and add comment and comment management functions. In the process of doing this big job, you will go back to see the previous tutorial, repeatedly read the Chinese document, will carefully read my code, when you finish the big job, Laravel 5 really get started ~ ~
The above mentioned is the whole content of this article, I hope you can like.
http://www.bkjia.com/PHPjc/981345.html www.bkjia.com true http://www.bkjia.com/PHPjc/981345.html techarticle Laravel 5 Framework Primer (d) End, Laravel end page and comments will use the "one-to-many relationship" provided by eloquent. In the end, we will get a prototype of a personal blogging system ...