码迷,mamicode.com
首页 > 其他好文 > 详细

laravel 增删改查

时间:2019-07-27 11:17:43      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:fun   ace   ade   ret   mes   修改   sql   xtend   dir   


路由
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 增删改查

标签:fun   ace   ade   ret   mes   修改   sql   xtend   dir   

原文地址:https://www.cnblogs.com/funbaby/p/11254243.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!