Laravel5 framework entry (4) and laravel. Laravel5 framework entry (4). The laavel end Page and comments will use the one-to-many relationship provided by Eloquent ". In the end, we will get the prototype of the individual blog system Laravel 5 framework entry (4 ).
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:
The 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:
The 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:
The code is as follows:
Route: post ('comment/store', 'commentscontroller @ store ');
Run the following command to create the CommentsController:
The 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 ')⬅Else back to homepage {$ 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
Script function reply (a) {var nickname =. parentNode. parentNode. firstChild. nextSibling. getAttribute ('data'); var textArea = document. getElementById ('newformcontent'); textArea. innerHTML = '@' + nickname + '';} script
@ Foreach ($ page-> hasManyComments as $ comment)
@ If ($ comment-> website) website }}" >{{ $ comment-> nickname }}@ else {{{ $ comment-> nickname }}@ endif {{$ comment-> created_at }}
{{$ Comment-> content }}
Reply
@ Endforeach
@ Endsection
The frontend comment function is complete.
View results:
5. background management
Modify basic view learnlaravel5/resources/views/app. blade. php:
Laravel
Toggle NavigationLearn Laravel 5
@ If (Auth: guest ())
- Login
- Register
@ Else
- {Auth: user ()-> name }}
@ Endif
@ Yield ('content ')