Laravel 5 framework entry (4) and laravel
Pages and comments will use the one-to-many relationship provided by Eloquent ". In the end, we will get a prototype of a personal blog system and assign a large assignment for practical practice.
1. First recognized Eloquent
Laravel Eloquent ORM is a very important part of Laravel and one of the reasons why Laravel is so popular. The Chinese document is:
1. http://laravel-china.org/docs/5.0/eloquent
2. http://www.golaravel.com/laravel/docs/5.0/eloquent/
In the previous tutorial, the created learnlaravel5/app/Page. php is a Eloquent Model class:
<?php namespace App;use Illuminate\Database\Eloquent\Model;class Page extends Model { //}
To learn more about Eloquent, read a series of articles: Laravel Eloquent
2. Create a Comment Model
First, create a new table to store Comment. Run the following command:
Copy codeThe Code is as follows:
Php artisan make: model Comment
After successful modification, the corresponding location of 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 codeThe Code is as follows:
Php artisan migrate
Go to the database. The comments table is already there.
3. Create a one-to-multiple relationship"
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'); }}
Okay ~ The relationship between models in Eloquent is so simple.
Relationship between models-http://laravel-china.org/docs/5.0/eloquent#relationships
4. frontend submission
Modify the Comment model:
<?php namespace App;use Illuminate\Database\Eloquent\Model;class Comment extends Model { protected $fillable = ['nickname', 'email', 'website', 'content', 'page_id'];}
Add a route entry:
Copy codeThe Code is as follows:
Route: post ('comment/store', 'commentscontroller @ store ');
Run the following command to create the CommentsController:
Copy codeThe Code is 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 () {if (Comment: create (Input: all () {return Redirect :: back ();} else {return Redirect: back ()-> withInput ()-> withErrors ('comment posting failed! ');}}}
Modify the view learnlaravel5/resources/views/pages/show. blade. php:
@ Extends ('_ layouts. default') @ section ('content')
The frontend comment function is complete.
View results:
5. Background Management
Modify basic view learnlaravel5/resources/views/app. blade. php:
<! DOCTYPE html>
Modify the backend route group (add a line ):
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 codeThe Code is as follows:
Php artisan make: controller Admin/CommentsController
Admin/CommentsController must have four interfaces: View All, view a single, modify POST, and delete:
<? 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 )); } Public function update (Request $ request, $ id) {$ this-> validate ($ request, ['nickname' => 'required ', 'content' => 'requestred',]); if (Comment: where ('id', $ id)-> update (Input :: response T (['_ 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/comments ');}}
Next, create two views:
Learnlaravel5/resources/views/admin/comments/index. blade. php:
@ Extends ('app') @ section ('content ') <div class = "container"> <div class = "row"> <div class = "col-md-10 col-md-offset-1"> <div class = "panel-default"> <div class = "row"> "panel-heading"> Manage comments </div> <div class = "panel-body"> <table class = "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"> edit </th> <th class = "col-lg-1"> Delete </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 }}">
Learnlaravel5/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-default"> <div class = "row"> "panel-heading"> edit comments </div> <div class = "panel-body"> @ if (count ($ errors)> 0) <div class = "alert-danger"> <strong> Whoops! </Strong> There were some problems with your input. <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-lg btn-info"> submit changes </button> </form> </div>> </div> @ endsection
The background management function is complete. view the results:
6. Large jobs
The Page-dependent comment function has all been completed, and the initial form of the personal blog system was born. At the end of this series of tutorials, We will assign a big job: to build the front and back of Article, add the one-to-many relationship between Article and Comment, and add the Comment and Comment management functions. In the process of doing this big job, you will go back to the previous tutorial repeatedly, read the Chinese document repeatedly, and read my code carefully. When you finish the big job, laravel 5 is getting started ~~
The above is all the content of this article. I hope you will like it.