laravel 框架简易增删改查
参看网址:http://www.yan.com/mou/add
图书增加HTML页面
//图书增加路由 Route::get('mou/add','MouController@store');
//控制器代码,这一步渲染添加的视图
public function add() { return view('mou.mouadd'); }
//添加视图HTML代码
<!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> <link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css"> </head> <body> <form action="/mou/insert" method="post" enctype="multipart/form-data" style="width: 300px;"> @csrf <div class="form-group"> <label for="name">标题</label> <input type="text" class="form-control" name="title" > </div> <div class="form-group"> <label for="name">作者</label> <input type="text" class="form-control" name="author" > </div> <div class="form-group"> <label for="name">书图</label> <input type="file" class="form-control" name="img" > </div> <div class="form-group"> <label for="name">分类</label> <input type="text" class="form-control" name="type" > </div> <input type="submit" value="立即添加"> </form> </body> </html>
//处理添加入库,数据库展示,删除,修改以及静态化详情控制器代码
<?php namespace App\Http\Controllers; use App\models\mouModel; use Illuminate\Http\Request; use QL\QueryList; class MouController extends Controller { // public function store() { //采集的地址 $url = 'https://www.hongxiu.com/search?kw=%E5%8E%86%E5%8F%B2'; $content = file_get_contents($url); $range = '.book-mid-info'; $rules = [ 'title' => ['h4', 'text'], 'author' => ['p[class=author]', 'text'], 'img' => ['img', 'src'], 'type' => ['a[target=_blank]', 'text'] ]; $data = QueryList::html($content) ->range($range) ->rules($rules) ->queryData(); foreach ($data as $k => $v) { $path = 'http:' . $v['img']; $file = file_get_contents($path); $filename = './book/' . md5($k) . '.jpg'; file_put_contents($filename, $file); } $res = mouModel::store($data); if ($res) { echo "<font color='red'>采集成功</font>"; header('refresh:2,url=/mou/list'); } else { echo "<font color='red'>采集失败</font>"; header('refresh:2,url=/mou/list'); } } //图书添加页面 public function add() { return view('mou.mouadd'); }
//图书入库 public function insert(Request $request) { // 接受参数 $params = $request->except('_token'); //处理图片 $path = '/' . $request->file('img')->store('img'); $params['img'] = $path; // 进行添加入库 $res = mouModel::store($params); if ($res) { echo "<font color='red'>添加成功</font>"; header('refresh:2,url=/mou/list'); } else { echo "<font color='red'>添加失败</font>"; header('refresh:2,url=/mou/add'); } } //展示 public function list(Request $request) { $word=$request->input('word'); $data = mouModel::list($word); return view('mou.moulist', compact('data','word')); } //删除 public function del($id) { // 这里可进行验证id不可以为空,是不是纯数字 $res = mouModel::del($id); if ($res) { echo "<font color='red'>删除成功</font>"; header('refresh:2,url=/mou/list'); } else { echo "<font color='red'>删除失败</font>"; header('refresh:2,url=/mou/list'); } } //详情展示 public function updataone($id) { // 这里可进行验证id不可以为空,是不是纯数字 $data = mouModel::updataone($id); return view('mou.mouupdataone', compact('data')); } //修改 public function updata(Request $request) { $params = $request->all(); $res = mouModel::updata($params); if ($res) { echo "<font color='red'>修改成功</font>"; header('refresh:2,url=/mou/list'); } else { echo "<font color='red'>修改失败</font>"; header('refresh:2,url=/mou/list'); } } //静态化详情 public function listone($id) { $filrname = "./book/" . md5($id) . ".html"; if (file_exists($filrname)) { echo file_get_contents($filrname); } else { $data = mouModel::listone($id); $html = view('mou.moulistone', compact('data')); file_put_contents($filrname, $html); return $html; } } }
//处理添加入库,数据库展示,删除,修改以及静态化详情模型代码
<?php namespace App\models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class mouModel extends Model { //软删除 use SoftDeletes; //定义数据表 protected $table = 'mou'; // 定义主键id public $primaryKey = 'id'; // 设置时间戳为关 public $timestamps = false; //添加入库的模型代码 public static function store($param) { return self::insert($param); } //展示 public static function list($word) { return self::where('title','like',"%$word%")->paginate(5); } //删除 public static function del($id) { return self::find($id)->delete(); } //修改展示页面 public static function updataone($id) { return self::find($id); } //修改 public static function updata($params) { $obj = self::find($params['updata_id']); $obj->title = $params['title']; $obj->author = $params['author']; $obj->img = $params['new_img']; $obj->type = $params['type']; return $obj->save(); } //详情展示 public static function listone($id){ return self::find($id); } }
//修改HTML代码:
<!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> <link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css"> </head> <body> <form action="/mou/updata" method="post" style="width: 300px;"> @csrf <div class="form-group"> <label for="name">标题</label> <input type="text" class="form-control" name="title" value="{{$data['title']}}" > </div> <div class="form-group"> <label for="name">作者</label> <input type="text" class="form-control" name="author" value="{{$data['author']}}" > </div> <div class="form-group"> <label for="name">书图</label> <img src="{{$data['img']}}" alt="无法显示"> <input type="file" value="new_img" name="new_img" > </div> <div class="form-group"> <label for="name">分类</label> <input type="text" class="form-control" name="type" value="{{$data['type']}}"> </div> {{-- 隐藏的id,传至控制器--}} <input type="hidden" value="{{$data['id']}}" name="updata_id"> <input type="submit" value="立即修改"> </form> </body> </html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现