Introduction to the Laravel 5 framework (iv) _php instance of the end article

Source: Internet
Author: User
Tags auth button type cloudflare

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. First acquaintance 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 has been established in the previous tutorial is a eloquent Model class:

<?php namespace App;

Use Illuminate\database\eloquent\model;

Class Page extends Model {

 //

}

To learn more about eloquent, recommend reading a series of articles: deep understanding of Laravel eloquent

2. Create Comment Model

First we want to create a new table to store the Comment, the command line to run:

Copy Code code as follows:

PHP Artisan Make:model Comment

After successful, the appropriate location to modify 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 ();
});

Run after:

Copy Code code as follows:

PHP Artisan Migrate

Go to the database and see, the comments watch 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 ');
 }



It's done. ~ 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 = [' nickname ', ' email ', ' website ', ' content ', ' page_id '];

}

Increase One Road by:

Copy Code code as follows:

Route::p ost (' Comment/store ', ' commentscontroller@store ');

Run the following command to create the Commentscontroller controller:

Copy 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 ()
 {
 if (comment::create input::all ( )) {return
  redirect::back ();
 } else {return redirect::back ()
  ->withinput ()->witherrors (' Comment published failed! ');
 }

 }

}

Modify View learnlaravel5/resources/views/pages/show.blade.php:

@extends (' _layouts.default ') @section (' content ')  

The front desk comment function is complete.

View Effects:

5. Background management function

Modify the underlying view learnlaravel5/resources/views/app.blade.php:

<! DOCTYPE html>  

To modify a background routing group (add 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 Code code as follows:

PHP Artisan Make:controller Admin/commentscontroller

Admin/commentscontroller to have View all, view single, post change, 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 ')->withco
 Mments (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 ', ' con
 Tent ' => ' required ',]); if (Comment::where (' id ', $id)->update (input::except ([' _method ', ' _token '))] {return redirect::to (' admin/
 Comments '); else {return redirect::back ()->withinput ()->witherrors () ' Update failed!
 ');
 The 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 Co L-md-offset-1 "> <div class=" Panel panel-default > <div class= "panel-heading" > Management review </div> & Lt;div class= "Panel-body" > <table class= "Table table-striped" > <tr class= "Row" > <th clas s= "col-lg-4" >Content</th> <th class= "col-lg-2" >User</th> <th class= "col-lg-4" >page&
     lt;/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 HRE
        F= ' {{$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 Co L-md-offset-1 "> <div class=" Panel Panel-default "> <div class=" panel-heading "> Editorial review </div> & Lt;div class= "Panel-body" > @if (Count ($errors) > 0) <div class= "alert Alert-danger" > <st Rong>whoops!</strong> There were some problems with your input.<br><br> <ul> @fo
      Reach ($errors->all () as $error) <li>{{$error}}</li> @endforeach </ul> </div> @endif <form action= "{{URL (' admin/comments/'. $comment->id)}}" method= "POST" > &L T;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: &LT;INP 
      UT 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=" "class=" Form-control "required" = "Required" >{{$comment->content}}</textarea> <br> <button class= "btn btn-lg btn-info" &G t; submit changes </button> </form> </div> </div> </div> </div> </div> @ends

 Ection

Background admin function Complete, view effect:

6. Big Job
Page-dependent comment functions have been completed, and the embryonic form 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, and add a one-to-many relationship between Article and Comment to add comment and comment management capabilities. In the process of doing this big job, you will go back to see the previous tutorial repeatedly, read the Chinese document repeatedly, will read my code carefully, when you finish the big homework, Laravel 5 is really getting started ~ ~

The above mentioned is the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.