laravel 增删改查
Posted funbaby
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了laravel 增删改查相关的知识,希望对你有一定的参考价值。
路由
Route::get(‘/mysql‘, ‘MysqlController@index‘);
Route::get(‘/mysql/add‘, ‘MysqlController@add‘);
Route::post(‘/mysql/insert‘, ‘MysqlController@insert‘);
Route::get(‘/mysql/edit/id‘, ‘MysqlController@edit‘)->where(‘id‘, ‘[0-9]+‘);
Route::post(‘/mysql/update‘, ‘MysqlController@update‘);
Route::get(‘/mysql/delete/id‘, ‘MysqlController@delete‘)->where(‘id‘, ‘[0-9]+‘);
MysqlController.php
<?php
namespace App\Http\Controllers;
use App\User;
use App\Mysql;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class MysqlController extends Controller
public function index(Request $sou)
$mysql = DB::table(‘mysql‘)->where(‘name‘,‘like‘,‘%‘.$sou->sou."%")->paginate(3);
return view(‘mysql.index‘, [‘mysql‘ => $mysql]);
public function add(Request $request)
return view(‘mysql.create‘);
public function insert(Request $request)
$res = DB::insert("INSERT INTO mysql(`name`,`pwd`,`age`,`sex`,`tel`) values(‘$request->name‘,‘$pwd‘,‘$request->age‘,‘$request->sex‘,‘$request->tel‘)");
if ($res)
return redirect(‘/mysql‘);
public function delete($id)
DB::table(‘mysql‘)->where(‘id‘,‘=‘,$id)->delete();
return redirect(‘/mysql‘);
public function edit($id)
$data = DB::table(‘mysql‘)->find($id);
return view(‘mysql.edit‘,[‘data‘=>$data]);
public function update(Request $request)
$res = DB::table(‘mysql‘)->where(‘id‘,‘=‘,$request->id)->update([‘name‘=>$request->name,‘pwd‘=>md5($request->pwd),‘age‘=>$request->age,‘sex‘=>$request->sex,‘tel‘=>$request->tel]);
if($res)
return redirect(‘/mysql‘);
?>
mysql/create.blade.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<center>
<form action="/mysql/insert" method="post">
@csrf
姓名<input type="text" name="name">
<br>
密码<input type="text" name="pwd">
<br>
年龄<input type="text" name="age">
<br>
性别<input type="text" name="sex">
<br>
电话<input type="text" name="tel">
<br>
<input type="submit" value="添加">
</form>
</center>
</body>
</html>
mysql/edit.blade.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<center>
<form action="/mysql/update" method="post">
@csrf
<input type="hidden" name="id" value="$data->id">
姓名<input type="text" name="name" value="$data->name">
<br>
密码<input type="password" name="pwd" value="$data->pwd">
<br>
年龄<input type="text" name="age" value="$data->age">
<br>
性别<input type="text" name="sex" value="$data->sex">
<br>
手机号<input type="tet" name="tel" value="$data->tel">
<br>
<input type="submit" value="修改">
</form>
</center>
</body>
</html>
index.blade.php
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" type="text/css" href="asset(‘css/app.css‘)">
<form action="" method="get">
<input type="text" name="sou" placeholder="请输入要搜索的内容">
<input type="submit" value="搜索">
</form>
<table class="table table-bordered">
<tr>
<td>ID</td>
<td>姓名</td>
<td>密码</td>
<td>年龄</td>
<td>性别</td>
<td>电话</td>
<td>操作</td>
<td>操作</td>
<td>操作</td>
</tr>
@foreach($mysql as $key=>$value)
<tr>
<td>$value->id</td>
<td>$value->name</td>
<td>$value->pwd</td>
<td>$value->age</td>
<td>$value->sex</td>
<td>$value->tel</td>
<td><a href="/mysql/add">添加</a></td>
<td><a href="/mysql/delete/$value->id">删除</a></td>
<td><a href="/mysql/edit/$value->id">编辑</a></td>
</tr>
@endforeach
</table>
$mysql->links()
以上是关于laravel 增删改查的主要内容,如果未能解决你的问题,请参考以下文章