本篇內(nèi)容介紹了“如何理解Laravel 5框架”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),淮安企業(yè)網(wǎng)站建設(shè),淮安品牌網(wǎng)站建設(shè),網(wǎng)站定制,淮安網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,淮安網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。1. 初識 Eloquent
Laravel Eloquent ORM 是 Laravel 中非常重要的部分,也是 Laravel 能如此流行的原因之一。中文文檔在:
1. http://laravel-china.org/docs/5.0/eloquent
2. http://www.golaravel.com/laravel/docs/5.0/eloquent/
在前面的教程中已經(jīng)建立好的 learnlaravel5/app/Page.php 就是一個 Eloquent Model 類:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Page extends Model { // }
若想進一步了解 Eloquent,推薦閱讀系列文章:深入理解 Laravel Eloquent
2. 創(chuàng)建 Comment 模型
首先我們要新建一張表來存儲 Comment,命令行運行:
復(fù)制代碼 代碼如下:
php artisan make:model Comment
成功以后,修改 migration 文件 learnlaravel5/database/migrations/***_create_comments_table.php 的相應(yīng)位置為:
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(); });
之后運行:
復(fù)制代碼 代碼如下:
php artisan migrate
去數(shù)據(jù)庫里瞧瞧,comments 表已經(jīng)躺在那兒啦。
3. 建立“一對多關(guān)系”
修改 Page 模型:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Page extends Model { public function hasManyComments() { return $this->hasMany('App\Comment', 'page_id', 'id'); } }
搞定啦~ Eloquent 中模型間關(guān)系就是這么簡單。
模型間關(guān)系中文文檔:http://laravel-china.org/docs/5.0/eloquent#relationships
4. 前臺提交功能
修改 Comment 模型:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Comment extends Model { protected $fillable = ['nickname', 'email', 'website', 'content', 'page_id']; }
增加一行路由:
復(fù)制代碼 代碼如下:
Route::post('comment/store', 'CommentsController@store');
運行以下命令創(chuàng)建 CommentsController 控制器:
復(fù)制代碼 代碼如下:
php artisan make:controller CommentsController
修改 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('評論發(fā)表失?。?#39;); } } }
修改視圖 learnlaravel5/resources/views/pages/show.blade.php:
@extends('_layouts.default') @section('content') <h5> <a href="/">??返回首頁</a> </h5> <h2 >{{ $page->title }}</h2> <hr> <div id="date" > {{ $page->updated_at }} </div> <div id="content" > <p> {{ $page->body }} </p> </div> <div id="comments" > @if (count($errors) > 0) <div class="alert alert-danger"> <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <div id="new"> <form action="{{ URL('comment/store') }}" method="POST"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <input type="hidden" name="page_id" value="{{ $page->id }}"> <div class="form-group"> <label>Nickname</label> <input type="text" name="nickname" class="form-control" required="required"> </div> <div class="form-group"> <label>Email address</label> <input type="email" name="email" class="form-control" > </div> <div class="form-group"> <label>Home page</label> <input type="text" name="website" class="form-control" > </div> <div class="form-group"> <label>Content</label> <textarea name="content" id="newFormContent" class="form-control" rows="10" required="required"></textarea> </div> <button type="submit" class="btn btn-lg btn-success col-lg-12">Submit</button> </form> </div> <script> function reply(a) { var nickname = a.parentNode.parentNode.firstChild.nextSibling.getAttribute('data'); var textArea = document.getElementById('newFormContent'); textArea.innerHTML = '@'+nickname+' '; } </script> <div class="conmments" > @foreach ($page->hasManyComments as $comment) <div class="one" > <div class="nickname" data="{{ $comment->nickname }}"> @if ($comment->website) <a href="{{ $comment->website }}"> <h4>{{ $comment->nickname }}</h4> </a> @else <h4>{{ $comment->nickname }}</h4> @endif <h7>{{ $comment->created_at }}</h7> </div> <div class="content"> <p > {{ $comment->content }} </p> </div> <div class="reply" > <a href="#new" onclick="reply(this);">回復(fù)</a> </div> </div> @endforeach </div> </div> @endsection
前臺評論功能完成。
查看效果:
5. 后臺管理功能
修改基礎(chǔ)視圖 learnlaravel5/resources/views/app.blade.php 為:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel</title> <link href="/css/app.css" rel="stylesheet"> <!-- Fonts --> <link href='http://fonts.useso.com/css?family=Roboto:400,300' rel='stylesheet' type='text/css'> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle Navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">Learn Laravel 5</a> </div> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li><a href="/admin">后臺首頁</a></li> </ul> <ul class="nav navbar-nav"> <li><a href="/admin/comments">管理評論</a></li> </ul> <ul class="nav navbar-nav navbar-right"> @if (Auth::guest()) <li><a href="/auth/login">Login</a></li> <li><a href="/auth/register">Register</a></li> @else <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">{{ Auth::user()->name }} <span class="caret"></span></a> <ul class="dropdown-menu" role="menu"> <li><a href="/auth/logout">Logout</a></li> </ul> </li> @endif </ul> </div> </div> </nav> @yield('content') <!-- Scripts --> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script> </body> </html>
修改后臺路由組(增加了一行):
Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'auth'], function() { Route::get('/', 'AdminHomeComtroller@index'); Route::resource('pages', 'PagesController'); Route::resource('comments', 'CommentsController'); });
創(chuàng)建 Admin\CommentsController :
復(fù)制代碼 代碼如下:
php artisan make:controller Admin/CommentsController
Admin/CommentsController 要有 查看所有、查看單個、POST更改、刪除四個接口:
<?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' => 'required', ]); if (Comment::where('id', $id)->update(Input::except(['_method', '_token']))) { return Redirect::to('admin/comments'); } else { return Redirect::back()->withInput()->withErrors('更新失?。?#39;); } } 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:
@extends('app') @section('content') <div class="container"> <div class="row"> <div class="col-md-10 col-md-offset-1"> <div class="panel panel-default"> <div class="panel-heading">管理評論</div> <div class="panel-body"> <table class="table 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">編輯</th> <th class="col-lg-1">刪除</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 }}"> <h5>{{ $comment->nickname }}</h5> </a> @else <h4>{{ $comment->nickname }}</h4> @endif {{ $comment->email }} </td> <td class="col-lg-4"> <a href="{{ URL('pages/'.$comment->page_id) }}" target="_blank"> {{ App\Page::find($comment->page_id)->title }} </a> </td> <td class="col-lg-1"> <a href="{{ URL('admin/comments/'.$comment->id.'/edit') }}" class="btn btn-success">編輯</a> </td> <td class="col-lg-1"> <form action="{{ URL('admin/comments/'.$comment->id) }}" method="POST" > <input name="_method" type="hidden" value="DELETE"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <button type="submit" class="btn btn-danger">刪除</button> </form> </td> </tr> @endforeach </table> </div> </div> </div> </div> </div> @endsection
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 panel-default"> <div class="panel-heading">編輯評論</div> <div class="panel-body"> @if (count($errors) > 0) <div class="alert alert-danger"> <strong>Whoops!</strong> There were some problems with your input.<br><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 btn-lg btn-info">提交修改</button> </form> </div> </div> </div> </div> </div> @endsection
后臺管理功能完成,查看效果:
“如何理解Laravel 5框架”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
網(wǎng)頁標題:如何理解Laravel5框架-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://jinyejixie.com/article18/dchdgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、手機網(wǎng)站建設(shè)、軟件開發(fā)、搜索引擎優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容