laravel(三)
一叉橱、composer
概念: 就是一個軟件挫以,通過該軟件管理項目所需工具庫,如驗證碼窃祝,上傳文件掐松,Excel表格導(dǎo)入導(dǎo)出等
中文官網(wǎng):https://www.phpcomposer.com
常用命令
?下載1:composer require 大名/小名
?下載2:設(shè)置composer.json文件 + composer install
?更新1:composer update 大名/小名
?更新2:設(shè)置composer.json文件 + composer update
?卸載1:composer remove 大名/小名
?卸載2:設(shè)置composer.json文件 + composer update
使用fake類生產(chǎn)假數(shù)據(jù)
從composer倉庫搜索工具庫 輸入命令進行下載
composer require fzaninotto/faker
使用:
<?php
// 引入autoload
require_once 'vendor/autoload.php';
//實例化類,通過調(diào)用屬性獲取假數(shù)據(jù)
// 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
#配置通過國內(nèi)鏡像下載
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
數(shù)據(jù)遷移(migration)
概念:就是使用PHP管理數(shù)據(jù)庫
語法\手冊
http://laravelacademy.org/post/6964.html
?php artisan make:migration 數(shù)據(jù)庫遷移名稱 (改表數(shù)據(jù))
?php artisan make:migration create_表名_table --create=表名 (創(chuàng)建表)
?php artisan make:migration update_表名_table --table=表名 (修改表)
?運行:php artisan migrate
?撤銷:php artisan migrate:rollback
創(chuàng)建表\修改表
刪除database\migrations下面默認(rèn)的兩個數(shù)據(jù)遷移文件,
#創(chuàng)建表
php artisan make: migration create_a_table --create=a
#在database 目錄下的migrations目錄中對應(yīng)的文件中添加
public function up()
{
Schema::create('a', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 60);
});
}
#執(zhí)行
php artisan migrate
修改表
php artisan make:migration update_a_table --table=a
#在database 目錄下的migrations目錄中對應(yīng)的文件中添加
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
通過數(shù)據(jù)遷移創(chuàng)建下屬表
原生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 '內(nèi)容',
created_at int not null default 0 comment '創(chuàng)建于',
updated_at int not null default 0 comment '更新于'
)engine=myisam charset=utf8;
數(shù)據(jù)遷移
創(chuàng)建數(shù)據(jù)遷移文件
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('內(nèi)容');
$table->integer('created_at')->default(0)->comment('創(chuàng)建于');
$table->integer('updated_at')->default(0)->comment('更新于');
});
}
php artisan migrate
填充數(shù)據(jù)(seeder)
數(shù)據(jù)遷移
就是用PHP管理數(shù)據(jù)庫
填充數(shù)據(jù)
就是用PHP來插入測試數(shù)據(jù)
語法
創(chuàng)建填充器: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('創(chuàng)建于');
$table->integer('updated_at')->default(0)->comment('更新于');
});
}
php artisan db:seed --class=Day3T2TableSeeder
faker使用
下載(注: 默認(rèn)已經(jīng)下載所有無需重復(fù)下載)
composer require fzaninotto/faker
可將上述代碼替換如下代碼即可
<?php
use Illuminate\Database\Seeder;
class Day3T2TableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
#需求:創(chuàng)建10條測試數(shù)據(jù)
#步驟1:實例化faker類型
$faker = \Faker\Factory::create('zh_CN');
#步驟2:插入測試數(shù)據(jù)
for ($i=1; $i<=10; $i++) {
//通過Day3T2TableSeeder填充器插入測試數(shù)據(jù)
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
學(xué)生管理小項目
說明:tinker是laravel自帶的便于調(diào)試數(shù)據(jù)庫的工具
語法: php artisan tinker
加密方式
Laravel存儲用戶密碼非MD5算法,而是使用php5.5新函數(shù)password_hash和password_verify
?加密語法:Hash::make('admin888'); 或者 bcrypt('admin888');
?校驗語法:Hash::check('admin888', 密文);
創(chuàng)建數(shù)據(jù)表
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('創(chuàng)建于');
$table->integer('updated_at')->default(0)->comment('更新于');
});
}
創(chuàng)建模型
php artisan make:model Http\Models\Student
在模型中添加
<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
/**
* 設(shè)置表名
* @var string
*/
protected $table = 'student';
/**
* 聲明可以批量設(shè)置的屬性
* @var array
*/
protected $fillable = ['uname', 'pwd', 'age', 'sex'];
/**
* Unix時間戳填充數(shù)據(jù)(注:默認(rèn)托管創(chuàng)建于更新于)
* @var string
*/
protected $dateFormat = 'U';
}
#指令:通過模型操作數(shù)據(jù)庫
#調(diào)用工廠文件:factory(App\Http\Models\Student::class, 2)->create()
#注:工廠文件中定義的模型必須通過migration創(chuàng)建無法將無法找到模型
登錄模塊[加載視圖]
步驟1:定義路由大磺,請求/student/login交給Student\LoginController@login
步驟2:創(chuàng)建Student\LoginController@login并加載視圖
步驟3:創(chuàng)建視圖文件
定義路由
Route::match(['get','post'],'student/login','Student/LoginController@login')
創(chuàng)建控制器
<?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');
}
}
創(chuàng)建視圖文件
<!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="/">開發(fā)</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="#">學(xué)習(xí)一</a></li>
<li><a href="#">學(xué)習(xí)二</a></li>
<li class="divider"></li>
<li><a href="#">技術(shù)學(xué)習(xí)</a></li>
<li class="divider"></li>
<li><a href="#">jq學(xué)習(xí)</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">相關(guān)<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>玩轉(zhuǎn)laravel-之登錄</h2>
<p> 玩轉(zhuǎn)- laravel</p>
</div>
</div>
<!-- 中間內(nèi)容區(qū)局 -->
<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>
修改控制器接受參數(shù)
<?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:接受數(shù)據(jù)
$postData = $request->only(['uname', 'pwd']); //數(shù)組
#步驟3:根據(jù)接受數(shù)據(jù)查詢用戶信息
$student = Student::where('uname', $postData['uname'])->first();
#步驟4:校驗
if (!$student) return redirect(url('/student/login'))->with(['state'=>'error', 'tips'=>'用戶不存在']);
//以前:md5($_POST['pwd']) != 數(shù)據(jù)庫[pwd]
//現(xiàn)在:Hash::check(用戶提交的密碼, 數(shù)據(jù)庫獲取的密碼);
if (!Hash::check($postData['pwd'], $student->pwd)) {
return redirect(url('/student/login'))->with(['state'=>'error', 'tips'=>'賬號或密碼錯誤']);
}
#步驟5:跳轉(zhuǎn)(成功-首頁抡句,失敗-登錄頁并彈框提示)
return redirect(url('/student/index'))->with(['state'=>'success', 'tips'=>'登錄成功']);
} else {
#步驟2:加載/resources/veiws/student/login/login.blade.php文件
return view('student.login.login');
}
}
}
學(xué)生列表
?步驟1:定義路由,請求/student/index交給Student\IndexController@index
?步驟2:創(chuàng)建控制器和方法并獲取數(shù)據(jù)杠愧、加載視圖
?步驟3:創(chuàng)建視圖并循環(huán)顯示數(shù)據(jù)
定義路由
Route::any('student/index','Student\IndexController@index')
創(chuàng)建控制器
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;
/**
* 學(xué)生管理控制器
* @author 班長
*/
class IndexController extends Controller
{
//學(xué)生列表
public function index()
{
#步驟1:數(shù)據(jù)庫獲取數(shù)據(jù)
$students = Student::orderBy('id', 'desc')->paginate(2);
#步驟2:加載視圖并傳遞數(shù)據(jù)
return view('student.index.index', compact('students'));
}
}
創(chuàng)建視圖并循環(huán)顯示數(shù)據(jù)
<!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="/">開發(fā)</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="#">學(xué)習(xí)一</a></li>
<li><a href="#">學(xué)習(xí)二</a></li>
<li class="divider"></li>
<li><a href="#">技術(shù)學(xué)習(xí)</a></li>
<li class="divider"></li>
<li><a href="#">jq學(xué)習(xí)</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">相關(guān)<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>玩轉(zhuǎn)laravel-之首頁</h2>
<p> 玩轉(zhuǎn)- laravel</p>
</div>
</div>
<!-- 中間內(nèi)容區(qū)局 -->
<div class="container">
<div class="row">
<!-- 左側(cè)菜單區(qū)域 -->
<div class="col-md-3">
<div class="list-group">
<a href="{{ url('student/index') }}" class="list-group-item active">學(xué)生列表</a>
<a href="{{ url('student/add') }}" class="list-group-item">新增學(xué)生</a>
</div>
</div>
<!-- 右側(cè)內(nèi)容區(qū)域 -->
<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>
<!-- 自定義內(nèi)容區(qū)域 -->
<div class="panel panel-default">
<div class="panel-heading">學(xué)生列表</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>
學(xué)生添加
?步驟1:定義路由流济,請求/student/add交給Student\IndexController@add
?步驟2:創(chuàng)建Student\IndexController@add并加載視圖
?步驟3:創(chuàng)建視圖文件
創(chuàng)建路由
Route::any('student/add','Student\IndexController@add')
創(chuàng)建控制器方法并加載視圖 接受數(shù)據(jù)
public function add(Request $request)
{
//判斷請求
if($request->isMethod('post')){
//接受參數(shù)
$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');
}
創(chuàng)建視圖文件 修改表單提交地址,屬性值等
<!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="/">開發(fā)</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="#">學(xué)習(xí)一</a></li>
<li><a href="#">學(xué)習(xí)二</a></li>
<li class="divider"></li>
<li><a href="#">技術(shù)學(xué)習(xí)</a></li>
<li class="divider"></li>
<li><a href="#">jq學(xué)習(xí)</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">相關(guān)<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>玩轉(zhuǎn)laravel</h2>
<p> 玩轉(zhuǎn)- laravel</p>
</div>
</div>
<!-- 中間內(nèi)容區(qū)局 -->
<div class="container">
<div class="row">
<!-- 左側(cè)菜單區(qū)域 -->
<div class="col-md-3">
<div class="list-group">
<a href="{{ url('student/index') }}" class="list-group-item ">學(xué)生列表</a>
<a href="javascript:void(0);" class="list-group-item active">新增學(xué)生</a>
</div>
</div>
<!-- 右側(cè)內(nèi)容區(qū)域 -->
<div class="col-md-9">
<!-- 所有的錯誤提示 -->
<div class="alert alert-danger">
<ul>
<li>姓名不能為空</li>
<li>年齡只能為整數(shù)</li>
<li>請選擇性別</li>
</ul>
</div>
<!-- 自定義內(nèi)容區(qū)域 -->
<div class="panel panel-default">
<div class="panel-heading">新增學(xué)生</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="請輸入學(xué)生姓名">
</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="請輸入學(xué)生年齡">
</div>
<div class="col-sm-5">
<p class="form-control-static text-danger">年齡只能為整數(shù)</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')}}", "系統(tǒng)提示","{{Session::get('state')}}")
@endif
</script>
</html>
學(xué)生修改
添加路由方法
Route::match(['get','post'],'/student/upd', 'Student\LoginController@upd');
修改連接地址附帶參數(shù)
<a href="{{ url("student/upd?id=$student->id ") }}">修改</a>
創(chuàng)建控制器方法接受并顯示視圖 修改數(shù)據(jù)
public function upd(Request $request)
{
$id = $request->input('id');
$data = Student::where('id',$id)->first();
if($request->isMethod('post')){
//接受參數(shù)
$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'));
}
展現(xiàn)視圖文件 附帶默認(rèn)值
<!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="/">開發(fā)</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="#">學(xué)習(xí)一</a></li>
<li><a href="#">學(xué)習(xí)二</a></li>
<li class="divider"></li>
<li><a href="#">技術(shù)學(xué)習(xí)</a></li>
<li class="divider"></li>
<li><a href="#">jq學(xué)習(xí)</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">相關(guān)<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>玩轉(zhuǎn)laravel</h2>
<p> 玩轉(zhuǎn)- laravel</p>
</div>
</div>
<!-- 中間內(nèi)容區(qū)局 -->
<div class="container">
<div class="row">
<!-- 左側(cè)菜單區(qū)域 -->
<div class="col-md-3">
<div class="list-group">
<a href="{{ url('student/index') }}" class="list-group-item ">學(xué)生列表</a>
<a href="{{ url('student/add') }}" class="list-group-item active">新增學(xué)生</a>
</div>
</div>
<!-- 右側(cè)內(nèi)容區(qū)域 -->
<div class="col-md-9">
<!-- 所有的錯誤提示 -->
<div class="alert alert-danger">
<ul>
<li>姓名不能為空</li>
<li>年齡只能為整數(shù)</li>
<li>請選擇性別</li>
</ul>
</div>
<!-- 自定義內(nèi)容區(qū)域 -->
<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="請輸入學(xué)生姓名" 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="請輸入學(xué)生年齡" value="{{ $data->age }}">
</div>
<div class="col-sm-5">
<p class="form-control-static text-danger">年齡只能為整數(shù)</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')}}", "系統(tǒng)提示","{{Session::get('state')}}")
@endif
</script>
</html>
學(xué)生刪除
修改連接地址附帶參數(shù)id
<a href="{{ url("student/del?id=$student->id ") }}" onclick="return confirm('確定要刪除嗎?')">刪除</a>
定義路由
Route::match(['get','post'],'/student/del', 'Student\LoginController@del');
創(chuàng)建控制器方法
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'=>'添加失敗']);
}
}