路由文件
//用户添加路由
Route::get('/user/add','UserController@add');
Route::post('/user/store','UserController@store');
Route::get('/user/index','UserController@index');
Route::get('/user/edit/{id}','UserController@edit');
Route::post('/user/save','UserController@save');
Route::get('/user/delete/{id}','UserController@delete');
Controller文件
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class UserController extends Controller
{
public function index(){
// $data = DB::table('person')->orderBy('id','desc')->get();
$data = DB::table('person')->select('id','name','age','email','phone')->get();
return view('person.list',['data'=>$data]);
}
public function add(){
return view('person.add');
}
public function store(Request $request){
$name = $request->input('name');
$age = $request->input('age');
$email = $request->input('email');
$phone = $request->input('phone');
$res = DB::table('person')->insert(['name'=>$name,'age'=>$age,'email'=>$email,'phone'=>$phone]);
if ($res){
echo json_encode(array('code'=>1,'msg'=>'添加成功'));
}else{
echo json_encode(array('code'=>0,'msg'=>'添加失败'));
}
}
public function edit($id){
$data = DB::table('person')->where('id',$id)->get();
return view('person.edit',['data'=>$data]);
}
public function save(Request $request){
$id = $request->input('id');
$name = $request->input('name');
$age = $request->input('age');
$email = $request->input('email');
$phone = $request->input('phone');
$res = DB::table('person')->where('id',$id)->update(['name'=>$name,'age'=>$age,'email'=>$email,'phone'=>$phone]);
if ($res){
echo json_encode(array('code'=>1,'msg'=>'编辑成功'));
}else{
echo json_encode(array('code'=>0,'msg'=>'编辑失败'));
}
}
public function delete($id){
$res = DB::table('person')->where('id',$id)->delete();
if ($res){
echo '<script>alert("删除成功");window.location.href="/user/index";</script>';
}else{
echo '<script>alert("删除失败");window.reload();</script>';
}
}
}
list界面
<!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>
<style>
a{text-decoration: none;}
</style>
</head>
<body>
<button><a href="/user/add">添加</a></button>
<table border="1" cellpadding="4" style="border-collapse: collapse;">
<caption>展示界面</caption>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>邮箱</th>
<th>电话</th>
<th>操作</th>
</tr>
@foreach($data as $item)
<tr>
<td>{{$item->id}}</td>
<td>{{$item->name}}</td>
<td>{{$item->age}}</td>
<td>{{$item->email}}</td>
<td>{{$item->phone}}</td>
<td><button><a href='/user/edit/{{$item->id}}'>编辑</a></button>
<button><a href='/user/delete/{{$item->id}}' onclick="if(confirm('是否删除数据?')==false)return false;">删除</a></button>
</td>
</tr>
@endforeach
</table>
<script>
</script>
</body>
</html>
add界面
<!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>
<script src="{{asset('js/jquery.min.js')}}"></script>
</head>
<body>
<form action="" name="add_form">
<table border="1" cellpadding="4" style="border-collapse:collapse;">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<caption>添加用户信息</caption>
<tr>
<td>姓名</td>
<td><input type="text" name="name" placeholder="请输入姓名"></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age" placeholder="请输入年龄"></td>
</tr>
<tr>
<td>邮箱</td>
<td><input type="text" name="email" placeholder="请输入邮箱"></td>
</tr>
<tr>
<td>电话</td>
<td><input type="text" name="phone" placeholder="请输入电话"></td>
</tr>
<tr>
<td colspan="2"><button type="button" onclick="doSave()">保存</button></td>
</tr>
</table>
</form>
<script>
function doSave() {
var name = $.trim($('input[name="name"]').val());
var age = $.trim($('input[name="age"]').val());
var email = $.trim($('input[name="email"]').val());
var phone = $.trim($('input[name="phone"]').val());
if (name==''){
alert('请输入姓名');
return;
}
if (age==''){
alert('请输入年龄');
return;
}
if (email==''){
alert('请输入邮箱');
return;
}
if (phone==''){
alert('请输入电话');
return;
}
$.post('/user/store',$('form[name="add_form"]').serialize(),function (res) {
if(res.code==0){
alert(res.msg);
return;
}else{
alert(res.msg);
window.location.href = '/user/index';
}
},'json');
}
</script>
</body>
</html>
edit界面
<!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>
<script src="{{asset('js/jquery.min.js')}}"></script>
</head>
<body>
<form action="" name="add_form">
<table border="1" cellpadding="4" style="border-collapse:collapse;">
<input type="hidden" name="_token" value="{{csrf_token()}}">
@foreach ($data as $item)
<input type="hidden" name="id" value="{{$item->id}}">
<caption>编辑用户信息</caption>
<tr>
<td>姓名</td>
<td><input type="text" name="name" value="{{$item->name}}"></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age" value="{{$item->age}}"></td>
</tr>
<tr>
<td>邮箱</td>
<td><input type="text" name="email" value="{{$item->email}}"></td>
</tr>
<tr>
<td>电话</td>
<td><input type="text" name="phone" value="{{$item->phone}}"></td>
</tr>
<tr>
<td colspan="2"><button type="button" onclick="doSave()">保存</button></td>
</tr>
@endforeach
</table>
</form>
<script>
function doSave() {
var name = $.trim($('input[name="name"]').val());
var age = $.trim($('input[name="age"]').val());
var email = $.trim($('input[name="email"]').val());
var phone = $.trim($('input[name="phone"]').val());
if (name==''){
alert('请输入姓名');
return;
}
if (age==''){
alert('请输入年龄');
return;
}
if (email==''){
alert('请输入邮箱');
return;
}
if (phone==''){
alert('请输入电话');
return;
}
$.post('/user/save',$('form[name="add_form"]').serialize(),function (res) {
if(res.code==0){
alert(res.msg);
return;
}else{
alert(res.msg);
window.location.href = '/user/index';
}
},'json');
}
</script>
</body>
</html>
where方法
传递三个参数,第一个参数是列名,第二个参数是任意一个数据库系统支持的运算符,第三个是该列要比较的值。例如:where(‘id’,’=’,’1’)
$data = DB::table('person')->where('id','=','3')->get();
whereIn方法
验证字段的值必须存在指定的数组里,两个参数,第一个参数是列名,第二个参数是数组,例如:whereIn(‘id’,[1,3,5])
$data = Db::table('person')->whereIn('id',[1,3,4])->get();
insert方法,返回是否插入成功,布尔值
插入记录到数据库中,接收数组形式的字段名和字段值进行插入操作。insert中可以接收一个数组,也可以接收多个数组
例如:insert([‘name’=>’张三’,’age’=>’24’])
insert([‘name’=>’张三’,’age’=>’24’],[‘name’=>’李四’,’age’=>’45’],[‘name’=>’王五’,’age’=>’68’])
$res = DB::table('person')->insert(['name'=>'小明','age'=>'26','email'=>'qwe@qq.com','phone'=>'10086']);
$res = DB::table('person')->insert(
['name'=>'小明','age'=>'26','email'=>'qwe@qq.com','phone'=>'10086'],['name'=>'小军','age'=>'32','email'=>'jun@qq.com','phone'=>'10086'],['name'=>'小红','age'=>'45','email'=>'hon@qq.com','phone'=>'10086']);
insertGetId,插入记录并返回id值
$res = DB::table('person')->insertGetId(['name'=>'小明','age'=>'26','email'=>'qwe@qq.com','phone'=>'10086']);
update更新方法
通过where增加更新条件,update里面用数组形式传值
$res = DB::table('person')->where('id',$id)->update(['name'=>$name,'age'=>$age,'email'=>$email,'phone'=>$phone]);
increment自增
第一个参数:需要修改的列。第二个参数是可选的,用于控制列递增的量
通常用在计算登录次数,点赞数等
$res = Db::table(‘person’)->increment(‘login_num’);
delete删除方法
$res = DB::table('person')->where('id',$id)->delete();
truncate清空表方法
删除所有行,并重置自增 ID 为零
$res = DB::table('person')->truncate()