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>
laravel 框架简易增删改查的更多相关文章
- tp框架的增删改查
首先,我们来看一下tp框架里面的查询方法: 查询有很多种,代码如下: <?php namespace Admin\Controller; use Think\Controller; class ...
- Yii2.0高级框架数据库增删改查的一些操作(转)
yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...
- Yii2.0高级框架数据库增删改查的一些操作
yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...
- Entity - 使用EF框架进行增删改查 - 模型先行
模型先行:先创建数据库实体模型,然后再进行数据库的增删改查. 基本步骤是不变的,可参照 <Entity - 使用EF框架进行增删改查 - 数据库先行> 其中的不同是,在创建数据库实体模型的 ...
- ssm 框架实现增删改查CRUD操作(Spring + SpringMVC + Mybatis 实现增删改查)
ssm 框架实现增删改查 SpringBoot 项目整合 一.项目准备 1.1 ssm 框架环境搭建 1.2 项目结构图如下 1.3 数据表结构图如下 1.4 运行结果 二.项目实现 1. Emplo ...
- FoxOne---一个快速高效的BS框架--生成增删改查
FoxOne---一个快速高效的BS框架--(1) FoxOne---一个快速高效的BS框架--(2) FoxOne---一个快速高效的BS框架--(3) FoxOne---一个快速高效的BS框架-- ...
- laravel orm进行增删改查
https://laravelacademy.org/post/9699.html 建议用DB门面直接操作数据库,因为ORM性能低.数据查询上面,ORM不会比DB差的,就比如with,是用了sql最基 ...
- 【项目笔记】完成一个基于SSM框架的增删改查的模块后总结的问题
最近为了准备新工作重新摸出了SSM框架,同时从0学习了JQuery,终于用一周做完了一个包括增删改查的模块(主要是属性太多了,其中一个类50+,复制粘贴耗时). 从中特意记下了几个遇到的问题,总结一下 ...
- MybatisMapper 映射框架(增删改查 原始模式)
//增删改查 package TestDemo; import java.io.IOException; import java.io.InputStream; import java.util.Da ...
随机推荐
- HttpClient的NoHttpResponse问题
调用第三方接口时会报NoHttpResponse异常,原因是上次的连接已经断掉了,但是客户端并未知道,复用上次连接就报错了,所以要解决这个问题,就是要校验上次链接是否断掉了 1. httpCl ...
- 取消a标签的默认行动(跳转到href)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue+element ui中select组件选择失效问题原因与解决方法
codejing 2020-07-10 09:13:31 652 收藏 分类专栏: Web Vue Element UI 版权 .当表单form赋完值后,如果后续又对form中某一属性值进行操作如 ...
- Solution -「Gym 102979L」 Lights On The Road
\(\mathcal{Description}\) Link. 给定序列 \(\{w_n\}\),选择 \(i\) 位置的代价为 \(w_i\),要求每个位置要不被选择,要不左右两个位置至少被 ...
- Solution -「多校联训」小卖部
\(\mathcal{Description}\) Link. 有 \(n\) 种物品,第 \(i\) 中有 \(a_i\) 个,单价为 \(b_i\).共 \(q\) 次询问,每次查询用不超 ...
- node(s) didn‘t match node selector.
k8s集群中,有pod出现了 Pending ,通过 kubectl describe pod 命令,发现了如下报错 0/4 nodes are available: 1 node(s) had ta ...
- Spring Cloud之微服务注册到Eureka Server集群
在Spring Cloud之服务注册中心搭建Eureka Server服务注册中⼼ - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)中已经搭建好了Eureka Server集群,本文就利用 ...
- MyBatisPlus分页不起作用?因为少了配置
/** @author QiuQiu&LL @create 2021-09-05 0:25 @Description: / @EnableTransactionManagement @Conf ...
- mysql5.7下载
官网:https://dev.mysql.com/doc/refman/5.7/en/installing.html 二进制安装:https://dev.mysql.com/doc/refman/5. ...
- windev的Trigger触发器,能秒SQL吗?
有朋友问,"你是不是在写论文?" (此处请想象个表情)"好吧,论文继续!" SQL中,触发器可以看成是一种特殊的存储过程,使用inserted临时表来建立数据关 ...