第一步:使用PHP終端創建一個名為blog的框架
composer create-project --prefer-dist laravel/laravel blog 7.x
創建好之后,在框架中找到resources目錄下的views檔案夾,在該檔案夾中創建一個名為form.blade.php表單
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>員工資訊</title>
<!-- 最新版本的 Bootstrap 核心 CSS 檔案 -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>
<body>
<form enctype="multipart/form-data" method="post" action="add">
@csrf
<div >
<label for="inputEmail3" >員工姓名</label>
<div >
<input type="text" name="username" id="inputEmail3" placeholder="員工姓名">
</div>
</div>
<div >
<label for="inputPassword3" >員工身份證號</label>
<div >
<input type="text" name="usercard" id="inputPassword3" placeholder="員工身份證號">
</div>
</div>
<div >
<label for="inputPassword3" >員工手機號</label>
<div >
<input type="text" name="usertel" id="inputPassword3" placeholder="員工手機號">
</div>
</div>
<div >
<div >
<button type="submit" >添加</button>
</div>
</div>
</form>
</body>
</html>
@if($errors->any())
@foreach($errors->all() as $value)
<p>{{ $value }}</p>
@endforeach
@endif
該頁面里的@csrf是為了不讓頁面出現419
下面這個是進行驗證form表單里面的非慷訓者進行正則驗證的時候,出現錯誤在表單下面提示錯誤
@if($errors->any())
@foreach($errors->all() as $value)
<p>{{ $value }}</p>
@endforeach
@endif
接下來在Navicat中創建一個名為2005a(舉的例子)的資料庫,并在該資料庫內創建一個cms的表

下一步,找到.env這個檔案,并把資料庫名字由原來的laravel改成創建好的2005a,DB_PASSWORD=為默認的root
使用終端創建一個名為UserProfile.php的控制器,控制器存放在框架app目錄下Http下Controllers檔案夾內
php artisan make:controller UserProfile
使用終端創建一個名為UserModel.php的模型
php artisan make:model UserModel
在模型內連接到表
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserModel extends Model
{
//連接2005a資料庫里的cms這個表
public $table='cms';
}
控制器內創建方法,并在routes檔案夾中的web.php中設定路由
<?php
namespace App\Http\Controllers;
use App\UserModel;
use Illuminate\Http\Request;
class UserProfile extends Controller
{
//添加方法
function add(Request $request){
$request->validate([
'username'=>'required',
'usercard'=>'required',
'usertel'=>'required'
],[
'username.required'=>'員工姓名不能為空',
'usercard.required'=>'員工身份證不能為空',
'usertel.required'=>'員工手機號不能為空'
]);
//接受資料
$param=$request->input();
unset($param['_token']);
//呼叫模型
$articel=new UserModel();
//添加
$res=$articel->insert($param);
if ($res){
return "<script>alert('添加成功');location.href='https://www.cnblogs.com/Boboschen/p/show'</script>";
}
return "<script>alert('失敗');location.href='https://www.cnblogs.com/Boboschen/p/form'</script>";
}
function show(){
$face= new UserModel();
$data=https://www.cnblogs.com/Boboschen/p/$face->get();
return view('show',['arr'=>$data]);
}
function index_del(Request $request){
$id=$request->input('id');
if (!is_numeric($id)){
return '引數不正確';
}
$articel=new UserModel();
$res=$articel->where('id',$id)->delete();
if ($res){
return "<script>alert('洗掉成功');location.href='https://www.cnblogs.com/Boboschen/p/show'</script>";
}
return redirect('show');
}
}
下面是路由
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('form',function (){
return view('form');
});
Route::post('add','UserProfile@add');
Route::get('show','UserProfile@show');
Route::get('delect','UserProfile@index_del');
還有一個展示的頁面,和form.blade.php檔案在同一個檔案夾內,創建一個名為show.blade.php展示頁面檔案
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<table border="1">
<tr>
<td>id</td>
<td>員工姓名</td>
<td>員工身份證</td>
<td>員工手機號</td>
<td>操作</td>
</tr>
@foreach($arr as $value)
<tr>
<td>{{ $value['id'] }}</td>
<td>{{ $value['username'] }}</td>
<td>{{ $value['usercard'] }}</td>
<td>{{ $value['usertel'] }}</td>
<td>
<a href="https://www.cnblogs.com/Boboschen/p/delect?id={{ $value['id'] }}">洗掉</a>
</td>
</tr>
@endforeach
</table>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/492227.html
標籤:PHP
下一篇:Laravel 將中文轉換為拼音
