laravel(三)
一、composer
概念: 就是一个软件,通过该软件管理项目所需工具库,如验证码,上传文件,Excel表格导入导出等
中文官网:https://www.phpcomposer.com
常用命令
下载1:composer require 大名/小名
下载2:设置composer.json文件 + composer install
更新1:composer update 大名/小名
更新2:设置composer.json文件 + composer update
卸载1:composer remove 大名/小名
卸载2:设置composer.json文件 + composer update
使用fake类生产假数据
从composer仓库搜索工具库 输入命令进行下载
composer require fzaninotto/faker
使用:
<?php
// 引入autoload
require_once 'vendor/autoload.php';
//实例化类,通过调用属性获取假数据
// use the factory to create a Faker\Generator instance
$faker = \Faker\Factory::create();
// generate data by accessing properties
echo $faker->name . '<hr >';
echo $faker->e164PhoneNumber . '<hr >';
echo $faker->phoneNumber . '<hr >';
echo $faker->tollFreePhoneNumber . '<hr >';
// 'Lucy Cechtelar';
echo $faker->address . '<hr >';
echo $faker->imageUrl($width = 130, $height = 130) . '<hr >';
// "426 Jordy Lodge
// Cartwrightshire, SC 88120-6700"
echo $faker->text . '<hr >';
// Dolores sit sint laboriosam dolorem culpa et autem. Beatae nam sunt fugit
// et sit et mollitia sed.
// Fuga deserunt tempora facere magni omnis. Om
通过composer安装laravel
#配置通过国内镜像下载
composer config -g repo.packagist composer https://packagist.phpcomposer.com
#通过composer下载框架
composer create-project laravel/laravel test_laravel
#-----------------------------------------------------------#
E:\phpStudy\PHPTutorial\php\php-7.2.1-nts\php.exe
C:\tools\composer\composer.phar
create-project laravel/laravel=5.6.* test_laravel2
数据迁移(migration)
概念:就是使用PHP管理数据库
语法\手册
http://laravelacademy.org/post/6964.html
php artisan make:migration 数据库迁移名称 (改表数据)
php artisan make:migration create_表名_table --create=表名 (创建表)
php artisan make:migration update_表名_table --table=表名 (修改表)
运行:php artisan migrate
撤销:php artisan migrate:rollback
创建表\修改表
删除database\migrations下面默认的两个数据迁移文件,
#创建表
php artisan make: migration create_a_table --create=a
#在database 目录下的migrations目录中对应的文件中添加
public function up()
{
Schema::create('a', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 60);
});
}
#执行
php artisan migrate
修改表
php artisan make:migration update_a_table --table=a
#在database 目录下的migrations目录中对应的文件中添加
public function up()
{
Schema::table('a', function (Blueprint $table) {
$table->string('name', 30)->change();
$table->integer('created_at');
});
}
php artisan migrate #会报错 要安装扩展
composer require doctrine/dbal
通过数据迁移创建下属表
原生sql
create table day3_t2 (
id int primary key auto_increment comment '编号',
uname varchar(30) not null comment '用户名',
pwd char(32) not null comment '密码',
content text not null comment '内容',
created_at int not null default 0 comment '创建于',
updated_at int not null default 0 comment '更新于'
)engine=myisam charset=utf8;
数据迁移
创建数据迁移文件
php artisan make:migration create_day3_t2_table --create=day3_t2
public function up()
{
Schema::create('day3_t2', function (Blueprint $table) {
$table->engine = 'myisam';
$table->increments('id')->comment('编号');
$table->string('uname', 30)->comment('用户名');
$table->char('pwd', 32)->comment('密码');
$table->text('content')->comment('内容');
$table->integer('created_at')->default(0)->comment('创建于');
$table->integer('updated_at')->default(0)->comment('更新于');
});
}
php artisan migrate
填充数据(seeder)
数据迁移
就是用PHP管理数据库
填充数据
就是用PHP来插入测试数据
语法
创建填充器:php artisan make:seeder 填充器名称 (注:表名TableSeeder 大驼峰)
运行填充器:php artisan db:seed --class=填充器名称
php artisan make:seeder Day3T2TableSeeder
public function up()
{
Schema::create('student', function (Blueprint $table) {
$table->increments('id')->comment('编号');
$table->string('uname', 45)->comment('用户名');
$table->string('pwd', 255)->comment('密码');
$table->unsignedTinyInteger('age')->default(0)->comment('年龄');
$table->unsignedTinyInteger('sex')->default(1)->comment('性别:1-男,2-女');
$table->integer('created_at')->default(0)->comment('创建于');
$table->integer('updated_at')->default(0)->comment('更新于');
});
}
php artisan db:seed --class=Day3T2TableSeeder
faker使用
下载(注: 默认已经下载所有无需重复下载)
composer require fzaninotto/faker
可将上述代码替换如下代码即可
<?php
use Illuminate\Database\Seeder;
class Day3T2TableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
#需求:创建10条测试数据
#步骤1:实例化faker类型
$faker = \Faker\Factory::create('zh_CN');
#步骤2:插入测试数据
for ($i=1; $i<=10; $i++) {
//通过Day3T2TableSeeder填充器插入测试数据
DB::table('day3_t2')->insert([
'uname' => $faker->name,
'pwd' => md5($faker->password),
'content' => $faker->paragraph(20),
'created_at' => time(),
'updated_at' => time()
]);
}
}
}
php artisan db:seed --class=Day3T2TableSeeder
学生管理小项目
说明:tinker是laravel自带的便于调试数据库的工具
语法: php artisan tinker
加密方式
Laravel存储用户密码非MD5算法,而是使用php5.5新函数password_hash和password_verify
加密语法:Hash::make('admin888'); 或者 bcrypt('admin888');
校验语法:Hash::check('admin888', 密文);
创建数据表
public function up()
{
Schema::create('student', function (Blueprint $table) {
$table->increments('id')->comment('编号');
$table->string('uname', 45)->comment('用户名');
$table->string('pwd', 255)->comment('密码');
$table->unsignedTinyInteger('age')->default(0)->comment('年龄');
$table->unsignedTinyInteger('sex')->default(1)->comment('性别:1-男,2-女');
$table->integer('created_at')->default(0)->comment('创建于');
$table->integer('updated_at')->default(0)->comment('更新于');
});
}
创建模型
php artisan make:model Http\Models\Student
在模型中添加
<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
/**
* 设置表名
* @var string
*/
protected $table = 'student';
/**
* 声明可以批量设置的属性
* @var array
*/
protected $fillable = ['uname', 'pwd', 'age', 'sex'];
/**
* Unix时间戳填充数据(注:默认托管创建于更新于)
* @var string
*/
protected $dateFormat = 'U';
}
#指令:通过模型操作数据库
#调用工厂文件:factory(App\Http\Models\Student::class, 2)->create()
#注:工厂文件中定义的模型必须通过migration创建无法将无法找到模型
登录模块[加载视图]
步骤1:定义路由,请求/student/login交给Student\LoginController@login
步骤2:创建Student\LoginController@login并加载视图
步骤3:创建视图文件
定义路由
Route::match(['get','post'],'student/login','Student/LoginController@login')
创建控制器
<?php
namespace App\Http\Controllers\Student;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
/**
* 登录控制器
* @author zhao@itcast.cn
*/
class LoginController extends Controller
{
//登录处理方法
public function login()
{
#加载/resources/veiws/student/login/login.blade.php文件
return view('student.login.login');
}
}
创建视图文件
<!DOCTYPE html>
<html lang="zh-CN">
<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>
<!-- Bootstrap CSS 文件 -->
<link rel="stylesheet" href="{{ asset('admin/static') }}/bootstrap/css/bootstrap.min.css">
<!-- 登录页面样式 -->
<link rel="stylesheet" href="{{ asset('admin/static') }}/bootstrap/css/login.css">
</head>
<body>
<div class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a href="/" class="navbar-brand">首页</a>
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#menu" aria-expanded="false">
<span class="sr-only">菜单折叠</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="menu">
<ul class="nav navbar-nav">
<li class="active"><a href="/">开发</a></li>
<li><a href="#">列表</a></li>
<li><a href="#">头条</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">资讯 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">学习一</a></li>
<li><a href="#">学习二</a></li>
<li class="divider"></li>
<li><a href="#">技术学习</a></li>
<li class="divider"></li>
<li><a href="#">jq学习</a></li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search...">
</div>
<button type="submit" class="btn btn-default">搜索</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="login.blade.php">登录</a></li>
<li><a href="./register.html">注册</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">相关<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">系列一</a></li>
<li class="divider"></li>
<li><a href="#">系列二</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<!-- 头部 -->
<div class="jumbotron jump">
<div class="container">
<h2>玩转laravel-之登录</h2>
<p> 玩转- laravel</p>
</div>
</div>
<!-- 中间内容区局 -->
<div class="container">
<form action="{{ url('student/login') }}" method="POST">
<div class="form-group">
<label for="username">用户名:</label>
<input type="text" name="uname" id="username" class="form-control" value="">
</div>
<div class="form-group">
<label for="password">密码:</label>
<input type="password" name="pwd" id="password" class="form-control" value="">
</div>
<div class="form-group">
<input type="submit" value="提交" class="btn btn-success">
<input type="reset" value="清空" class="btn btn-danger">
</div>
</form>
</div>
<!-- 尾部 -->
<div class="jumbotron" style="margin:0;">
<div class="container">
<span> @2017 laravel</span>
</div>
</div>
<!-- jQuery 文件 -->
<script src="{{ asset('admin/static') }}/jquery/jquery.min.js"></script>
<!-- Bootstrap JavaScript 文件 -->
<script src="{{ asset('admin/static') }}/bootstrap/js/bootstrap.min.js"></script>
</body>
<link rel="stylesheet" type="text/css" href="{{ asset('css') }}/sweetalert.min.css">
<script type="text/javascript" src="{{ asset('js') }}/sweetalert.min.js"></script>
<script type="text/javascript">
@if (Session::has('tips'))
swal("{{ Session::get('tips') }}", "提示","{{ Session::get('state') }}")
@endif
</script>
</html>
修改控制器接受参数
<?php
namespace App\Http\Controllers\Student;
use Hash;
use App\Http\Models\Student;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
/**
* 登录控制器
* @author zhao@itcast.cn
*/
class LoginController extends Controller
{
//登录处理方法
public function login(Request $request)
{
#步骤1:判断请求方式
if ($request->isMethod('post')) {
#步骤2:接受数据
$postData = $request->only(['uname', 'pwd']); //数组
#步骤3:根据接受数据查询用户信息
$student = Student::where('uname', $postData['uname'])->first();
#步骤4:校验
if (!$student) return redirect(url('/student/login'))->with(['state'=>'error', 'tips'=>'用户不存在']);
//以前:md5($_POST['pwd']) != 数据库[pwd]
//现在:Hash::check(用户提交的密码, 数据库获取的密码);
if (!Hash::check($postData['pwd'], $student->pwd)) {
return redirect(url('/student/login'))->with(['state'=>'error', 'tips'=>'账号或密码错误']);
}
#步骤5:跳转(成功-首页,失败-登录页并弹框提示)
return redirect(url('/student/index'))->with(['state'=>'success', 'tips'=>'登录成功']);
} else {
#步骤2:加载/resources/veiws/student/login/login.blade.php文件
return view('student.login.login');
}
}
}
学生列表
步骤1:定义路由,请求/student/index交给Student\IndexController@index
步骤2:创建控制器和方法并获取数据、加载视图
步骤3:创建视图并循环显示数据
定义路由
Route::any('student/index','Student\IndexController@index')
创建控制器
php artisan make:controller Student\IndexController
<?php
namespace App\Http\Controllers\Student;
use App\Http\Models\Student;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
/**
* 学生管理控制器
* @author 班长
*/
class IndexController extends Controller
{
//学生列表
public function index()
{
#步骤1:数据库获取数据
$students = Student::orderBy('id', 'desc')->paginate(2);
#步骤2:加载视图并传递数据
return view('student.index.index', compact('students'));
}
}
创建视图并循环显示数据
<!DOCTYPE html>
<html lang="zh-CN">
<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>
<!-- Bootstrap CSS 文件 -->
<link rel="stylesheet" href="{{ asset('admin/static') }}/bootstrap/css/bootstrap.min.css">
<!-- 首页单独样式 -->
<link rel="stylesheet" href="{{ asset('admin/static') }}/bootstrap/css/index.css">
</head>
<body>
<div class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a href="/" class="navbar-brand">首页</a>
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#menu" aria-expanded="false">
<span class="sr-only">菜单折叠</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="menu">
<ul class="nav navbar-nav">
<li class="active"><a href="/">开发</a></li>
<li><a href="#">列表</a></li>
<li><a href="#">头条</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">资讯 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">学习一</a></li>
<li><a href="#">学习二</a></li>
<li class="divider"></li>
<li><a href="#">技术学习</a></li>
<li class="divider"></li>
<li><a href="#">jq学习</a></li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search...">
</div>
<button type="submit" class="btn btn-default">搜索</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="login.blade.php">登录</a></li>
<li><a href="./register.html">注册</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">相关<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">系列一</a></li>
<li class="divider"></li>
<li><a href="#">系列二</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<!-- 头部 -->
<div class="jumbotron jump">
<div class="container">
<h2>玩转laravel-之首页</h2>
<p> 玩转- laravel</p>
</div>
</div>
<!-- 中间内容区局 -->
<div class="container">
<div class="row">
<!-- 左侧菜单区域 -->
<div class="col-md-3">
<div class="list-group">
<a href="{{ url('student/index') }}" class="list-group-item active">学生列表</a>
<a href="{{ url('student/add') }}" class="list-group-item">新增学生</a>
</div>
</div>
<!-- 右侧内容区域 -->
<div class="col-md-9">
<!-- 成功提示框 -->
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<strong>成功!</strong> 操作成功提示!
</div>
<!-- 失败提示框 -->
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<strong>失败!</strong> 操作失败提示!
</div>
<!-- 自定义内容区域 -->
<div class="panel panel-default">
<div class="panel-heading">学生列表</div>
<table class="table table-striped table-hover table-responsive">
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>添加时间</th>
<th width="120">操作</th>
</tr>
</thead>
<tbody>
@foreach($students as $student)
<tr>
<th scope="row">{{ $student->id }}</th>
<td>{{ $student->uname }}</td>
<td>{{ $student->age }}</td>
<td>
@if($student->sex == 1)
男
@else
女
@endif
</td>
<td>{{ $student->created_at }}</td>
<td>
<a href="">详情</a>
<a href="{{ url("student/upd?id=$student->id ") }}">修改</a>
<a href="{{ url("student/del?id=$student->id ") }}" onclick="return confirm('确定要删除吗?')">删除</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<!-- 分页 -->
<div>
{{ $students->links() }}
</div>
</div>
</div>
</div>
<!-- 尾部 -->
<div class="jumbotron" style="margin:0;">
<div class="container">
<span> @2017 laravel</span>
</div>
</div>
<!-- jQuery 文件 -->
<script src="{{ asset('admin/static') }}/jquery/jquery.min.js"></script>
<!-- Bootstrap JavaScript 文件 -->
<script src="{{ asset('admin/static') }}/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
学生添加
步骤1:定义路由,请求/student/add交给Student\IndexController@add
步骤2:创建Student\IndexController@add并加载视图
步骤3:创建视图文件
创建路由
Route::any('student/add','Student\IndexController@add')
创建控制器方法并加载视图 接受数据
public function add(Request $request)
{
//判断请求
if($request->isMethod('post')){
//接受参数
$data = $request->only(['uname','pwd','age','sex']);
$data['pwd'] = bcrypt($data['pwd']);
#dd($data);
$rs = Student::create($data);
if($rs){
return redirect(url('/student/index'))->with(['state'=>'success','tips'=>'添加成功']);
}else{
return redirect(url('/student/add'))->with(['state'=>'error','tips'=>'添加失败']);
}
}
return view('admin.student.add');
}
创建视图文件 修改表单提交地址,属性值等
<!DOCTYPE html>
<html lang="zh-CN">
<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>
<!-- Bootstrap CSS 文件 -->
<link rel="stylesheet" href="{{ asset('admin/static') }}/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="{{ asset('admin/static') }}/bootstrap/css/form.css">
</head>
<body>
<div class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a href="/" class="navbar-brand">首页</a>
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#menu" aria-expanded="false">
<span class="sr-only">菜单折叠</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="menu">
<ul class="nav navbar-nav">
<li class="active"><a href="/">开发</a></li>
<li><a href="#">列表</a></li>
<li><a href="#">头条</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">资讯 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">学习一</a></li>
<li><a href="#">学习二</a></li>
<li class="divider"></li>
<li><a href="#">技术学习</a></li>
<li class="divider"></li>
<li><a href="#">jq学习</a></li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search...">
</div>
<button type="submit" class="btn btn-default">搜索</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="./login.html">登录</a></li>
<li><a href="./register.html">注册</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">相关<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">系列一</a></li>
<li class="divider"></li>
<li><a href="#">系列二</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<!-- 头部 -->
<div class="jumbotron jump">
<div class="container">
<h2>玩转laravel</h2>
<p> 玩转- laravel</p>
</div>
</div>
<!-- 中间内容区局 -->
<div class="container">
<div class="row">
<!-- 左侧菜单区域 -->
<div class="col-md-3">
<div class="list-group">
<a href="{{ url('student/index') }}" class="list-group-item ">学生列表</a>
<a href="javascript:void(0);" class="list-group-item active">新增学生</a>
</div>
</div>
<!-- 右侧内容区域 -->
<div class="col-md-9">
<!-- 所有的错误提示 -->
<div class="alert alert-danger">
<ul>
<li>姓名不能为空</li>
<li>年龄只能为整数</li>
<li>请选择性别</li>
</ul>
</div>
<!-- 自定义内容区域 -->
<div class="panel panel-default">
<div class="panel-heading">新增学生</div>
<div class="panel-body">
<form class="form-horizontal" action="{{ url('student/add') }}" method="post">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">姓名</label>
<div class="col-sm-5">
<input type="text" class="form-control" name="uname" id="name" placeholder="请输入学生姓名">
</div>
<div class="col-sm-5">
<p class="form-control-static text-danger">姓名不能为空</p>
</div>
</div>
<div class="form-group">
<label for="age" class="col-sm-2 control-label">密码</label>
<div class="col-sm-5">
<input type="text" class="form-control" name="pwd" id="age" placeholder="请输入密码">
</div>
<div class="col-sm-5">
<p class="form-control-static text-danger">密码不能为空</p>
</div>
</div>
<div class="form-group">
<label for="age" class="col-sm-2 control-label">年龄</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="age" name="age" placeholder="请输入学生年龄">
</div>
<div class="col-sm-5">
<p class="form-control-static text-danger">年龄只能为整数</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">性别</label>
<div class="col-sm-5">
<label class="radio-inline">
<input type="radio" name="sex" value="1" > 男
</label>
<label class="radio-inline">
<input type="radio" name="sex" value="2" checked="checked"> 女
</label>
</div>
<div class="col-sm-5">
<p class="form-control-static text-danger">请选择性别</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">提交</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<!-- 尾部 -->
<div class="jumbotron" style="margin:0;">
<div class="container">
<span> @2017 laravel</span>
</div>
</div>
<!-- jQuery 文件 -->
<script src="{{ asset('admin/static') }}/jquery/jquery.min.js"></script>
<!-- Bootstrap JavaScript 文件 -->
<script src="{{ asset('admin/static') }}/bootstrap/js/bootstrap.min.js"></script>
</body>
<link rel="stylesheet" type="text/css" href="{{ asset('css') }}/sweetalert.min.css">
<script type="text/javascript" src="{{ asset('js') }}/sweetalert.min.js"></script>
<script type="text/javascript">
@if (Session::has('tips'))
swal("{{Session::get('tips')}}", "系统提示","{{Session::get('state')}}")
@endif
</script>
</html>
学生修改
添加路由方法
Route::match(['get','post'],'/student/upd', 'Student\LoginController@upd');
修改连接地址附带参数
<a href="{{ url("student/upd?id=$student->id ") }}">修改</a>
创建控制器方法接受并显示视图 修改数据
public function upd(Request $request)
{
$id = $request->input('id');
$data = Student::where('id',$id)->first();
if($request->isMethod('post')){
//接受参数
$postdata = $request->only(['uname','pwd','age','sex']);
$postdata['pwd'] = bcrypt($postdata['pwd']);
//echo $id;
//dd($postdata);
$rs = Student::where('id' , $id)->update($postdata);
if($rs){
return redirect(url('/student/index'))->with(['state'=>'success','tips'=>'添加成功']);
}else{
return redirect(url('/student/upd'))->with(['state'=>'error','tips'=>'添加失败']);
}
}
return view('admin.student.upd',compact('data'));
}
展现视图文件 附带默认值
<!DOCTYPE html>
<html lang="zh-CN">
<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>
<!-- Bootstrap CSS 文件 -->
<link rel="stylesheet" href="{{ asset('admin/static') }}/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="{{ asset('admin/static') }}/bootstrap/css/form.css">
</head>
<body>
<div class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a href="/" class="navbar-brand">首页</a>
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#menu" aria-expanded="false">
<span class="sr-only">菜单折叠</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="menu">
<ul class="nav navbar-nav">
<li class="active"><a href="/">开发</a></li>
<li><a href="#">列表</a></li>
<li><a href="#">头条</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">资讯 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">学习一</a></li>
<li><a href="#">学习二</a></li>
<li class="divider"></li>
<li><a href="#">技术学习</a></li>
<li class="divider"></li>
<li><a href="#">jq学习</a></li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search...">
</div>
<button type="submit" class="btn btn-default">搜索</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="./login.html">登录</a></li>
<li><a href="./register.html">注册</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">相关<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">系列一</a></li>
<li class="divider"></li>
<li><a href="#">系列二</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<!-- 头部 -->
<div class="jumbotron jump">
<div class="container">
<h2>玩转laravel</h2>
<p> 玩转- laravel</p>
</div>
</div>
<!-- 中间内容区局 -->
<div class="container">
<div class="row">
<!-- 左侧菜单区域 -->
<div class="col-md-3">
<div class="list-group">
<a href="{{ url('student/index') }}" class="list-group-item ">学生列表</a>
<a href="{{ url('student/add') }}" class="list-group-item active">新增学生</a>
</div>
</div>
<!-- 右侧内容区域 -->
<div class="col-md-9">
<!-- 所有的错误提示 -->
<div class="alert alert-danger">
<ul>
<li>姓名不能为空</li>
<li>年龄只能为整数</li>
<li>请选择性别</li>
</ul>
</div>
<!-- 自定义内容区域 -->
<div class="panel panel-default">
<div class="panel-heading">修改信息</div>
<div class="panel-body">
<form class="form-horizontal" action="{{ url('student/upd') }}" method="post">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">姓名</label>
<input type="hidden" name="id" value="{{ $data->id }}">
<div class="col-sm-5">
<input type="text" class="form-control" name="uname" id="name" placeholder="请输入学生姓名" value="{{ $data->uname }}">
</div>
<div class="col-sm-5">
<p class="form-control-static text-danger">姓名不能为空</p>
</div>
</div>
<div class="form-group">
<label for="age" class="col-sm-2 control-label">密码</label>
<div class="col-sm-5">
<input type="password" class="form-control" name="pwd" placeholder="请输入密码">
</div>
<div class="col-sm-5">
<p class="form-control-static text-danger">密码不能为空</p>
</div>
</div>
<div class="form-group">
<label for="age" class="col-sm-2 control-label">年龄</label>
<div class="col-sm-5">
<input type="text" class="form-control" name="age" placeholder="请输入学生年龄" value="{{ $data->age }}">
</div>
<div class="col-sm-5">
<p class="form-control-static text-danger">年龄只能为整数</p>
</div>
</div>
<td>
@if($data->sex == 1)
<div class="form-group">
<label class="col-sm-2 control-label">性别</label>
<div class="col-sm-5">
<label class="radio-inline">
<input type="radio" name="sex" value="1" id="sex_1" checked="checked"> 男
</label>
<label class="radio-inline">
<input type="radio" name="sex" value="2" id="sex_2" > 女
</label>
</div>
<div class="col-sm-5">
<p class="form-control-static text-danger">请选择性别</p>
</div>
</div>
@else
<div class="form-group">
<label class="col-sm-2 control-label">性别</label>
<div class="col-sm-5">
<label class="radio-inline">
<input type="radio" name="sex" value="1" id="sex_1"> 男
</label>
<label class="radio-inline">
<input type="radio" name="sex" value="2" id="sex_2" checked="checked"> 女
</label>
</div>
<div class="col-sm-5">
<p class="form-control-static text-danger">请选择性别</p>
</div>
</div>
@endif
</td>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">提交</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<!-- 尾部 -->
<div class="jumbotron" style="margin:0;">
<div class="container">
<span> @2017 laravel</span>
</div>
</div>
<!-- jQuery 文件 -->
<script src="{{ asset('admin/static') }}/jquery/jquery.min.js"></script>
<!-- Bootstrap JavaScript 文件 -->
<script src="{{ asset('admin/static') }}/bootstrap/js/bootstrap.min.js"></script>
</body>
<link rel="stylesheet" type="text/css" href="{{ asset('css') }}/sweetalert.min.css">
<script type="text/javascript" src="{{ asset('js') }}/sweetalert.min.js"></script>
<script type="text/javascript">
@if (Session::has('tips'))
swal("{{Session::get('tips')}}", "系统提示","{{Session::get('state')}}")
@endif
</script>
</html>
学生删除
修改连接地址附带参数id
<a href="{{ url("student/del?id=$student->id ") }}" onclick="return confirm('确定要删除吗?')">删除</a>
定义路由
Route::match(['get','post'],'/student/del', 'Student\LoginController@del');
创建控制器方法
public function del(Request $request)
{
$id = $request->input('id');
$rs = Student::where('id',$id)->delete();
if($rs){
return redirect(url('/student/index'))->with(['state'=>'success','tips'=>'添加成功']);
}else{
return redirect(url('/student/del'))->with(['state'=>'error','tips'=>'添加失败']);
}
}
网友评论