做个文章的增删改查
第一步 把数据库的表结构建好,生成迁移
1 怎么建,当然是用php artisan命令了
使用 Artisan 命令 make:migration 来创建一个新的迁移:
php artisan make:migration create_article_table
2 新的迁移位于 database/migrations 目录下,每个迁移文件名都包含时间戳从而允许 Laravel 判断其顺序。
找到 2017_08_12_021758_create_article_table.php 文件
在public function up 把建表语句建好
public function up() { Schema::create('article', function (Blueprint $table) { $table->increments('id'); $table->string('title', 100); $table->text('description'); $table->timestamps(); }); }
public function down 就是删除表
public function down() { Schema::drop('article'); }
3 运行数据迁移 就是建表语句的创建
php artisan migrate
默认的 user 表和password_resets 报错 把 user表的unique和password表的创建索引给去掉
执行成功,查看article表创建完毕
表建立完毕了,那么用系统自带的工具 orm模型 自动创建增删改
1创建模型,还是使用命令
php artisan make:model Article
生成的文件在 blog\app\下
2 模型建立完毕,可以获取模型了
为了方便测试,手工在数据库的article表中,随便插入2条数据,
然后在控制器中 调用模型中的数据 来查看
在 blog\app\Article.php 中 增加表的名字
protected $table = 'article';
3 在控制器调用数据看下
控制器顶部增加 use App\Article;
函数中增加
$articles = Article::all();
foreach ($articles as $article) {
echo $article->title;
}
4 查看下数据
好了,基础的测试通了,咱们开始增删改了
1 先给article创建个控制器
php artisan make:controller ArticleController
生成的文件在 blog\app\Http\Controllers 这个目录下
2 设置路由规则 可访问 articlecontroller的函数
在blog/routes/web.php
Route::resource('article', 'ArticleController'); Route::get('article/show/{id}', 'ArticleController@show');
添加如下 第一个是设置文章控制器和内部的方法可用
第二个是设置URL 访问文章控制器的show方法
3 填充 文章控制器
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Article; class ArticleController extends Controller { /** * 添加文章 */ public function store(Request $request) { $article = new Article; $article->title = $request->input('title'); $article->description = $request->input('description'); if ($article->save()) { return redirect('article'); } else { return back()->withInput()->withErrors('保存失败'); } } /** * 更新文章 */ public function update(Request $request,$id) { $article = Article::find($id); $article->title = $request->input('title'); $article->description = $request->input('description'); if ($article->save()) { return redirect('article'); } else { return back()->withInput()->withErrors('保存失败'); } } /** 文章添加展示页 **/ public function create() { return view('article.create'); } //文章更新展示页 public function edit($id) { $article=Article::find($id); return view('article.edit',compact('article')); } //文章删除 public function destroy($id) { $article = Article::find($id); $article->delete(); return redirect('article'); } //文章列表 public function index() { $articles = Article::all(); return view('article.index',compact('articles')); } //文章显示 public function show($id) { $article=Article::find($id); return view('article.show',compact('article')); } }
4 设置模板页
模板页面位置在blog\resources\views 这个位置
1)在views\layouts\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>test</title>
<link href="/css/app.css" rel="stylesheet">
</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="#">后台管理系统</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="/home">后台首页</a></li>
<ul class="nav navbar-nav">
<li><a href="/article">管理文章</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
@if (Auth::guest())
<li><a href="{{ route('login') }}">登录</a></li>
<li><a href="{{ route('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="{{ route('logout') }}" onclick="event.preventDefault();
document.getElementById('logout-form').submit();">退出</a></li>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
</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>
2)我们新建一个article文件夹
新建4个模板文件
create.blade.php
@extends('layouts.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('article') }}" enctype="multipart/form-data" method="POST"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> 文章标题:<input type="text" name="title" class="form-control" required="required"> <br> <br> 文章内容: <textarea name="description" rows="10" class="form-control"></textarea> <br> <button class="btn btn-lg btn-info">新增文章</button> </form> </div> </div> </div> </div> </div> @endsection
edit.blade.php
@extends('layouts.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('article/'.$article->id) }}" enctype="multipart/form-data" method="POST"> <input name="_method" type="hidden" value="PUT"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> 文章标题:<input type="text" name="title" class="form-control" required="required" value="{{ $article->title }}"> <br> <br> <br> 文章内容:<textarea name="description" rows="10" class="form-control" required="required">{{ $article->description }}</textarea> <br> <button class="btn btn-lg btn-info">编辑文章</button> </form> </div> </div> </div> </div> </div> @endsection
index.blade.php
@extends('layouts.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"> <a href="{{ URL('article/create') }}" class="btn btn-lg btn-primary">新增</a> <table class="table table-striped"> <tr class="row"> <th class="col-lg-4">内容</th> <th class="col-lg-2">标题</th> <th class="col-lg-4">查看</th> <th class="col-lg-1">编辑</th> <th class="col-lg-1">删除</th> </tr> @foreach ($articles as $article) <tr class="row"> <td class="col-lg-6"> {{ $article->description }} </td> <td class="col-lg-2"> {{ $article->title }} </td> <td class="col-lg-4"> <a href="{{ URL('article/show/'.$article->id) }}" target="_blank"> {{ App\Article::find($article->id)->title }} </a> </td> <td class="col-lg-1"> <a href="{{ URL('article/'.$article->id.'/edit') }}" class="btn btn-success">编辑</a> </td> <td class="col-lg-1"> <form action="{{ URL('article/'.$article->id) }}" method="POST" style="display: inline;"> <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
show.blade.php
@extends('layouts.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 <input name="_method" type="hidden" value="PUT"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> 文章标题:{{ $article->title }} <br> <br> <br> 文章内容:{{ $article->description }} <br> </div> </div> </div> </div> </div> @endsection
5 登录后台 操作增删改查
源码文件在此,欢迎对照查阅
https://github.com/baker95935/laravelPractise5.4