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 ...
随机推荐
- .NET Core分析程序集最优美的方法,不用Assembly.LoadFile(),超越ReflectionOnlyLoad
在编写.NET程序的时候,如果需要对一个程序集文件进行分析,我们可以使用Assembly.LoadFile()来加载这个程序集,然后对LoadFile()方法返回的Assembly对象进行进一步的分析 ...
- spring学习三:Spring Bean 生命周期
Bean 的生命周期 理解 Spring bean 的生命周期很容易.当一个 bean 被实例化时,它可能需要执行一些初始化使它转换成可用状态.同样,当 bean 不再需要,并且从容器中移除时,可能需 ...
- JAVA多线程学习十一-线程锁技术
前面我们讲到了synchronized:那么这节就来将lock的功效. 一.locks相关类 锁相关的类都在包java.util.concurrent.locks下,有以下类和接口: |---Abst ...
- JS 将Table内容导出到Excel(样式设计)
转载请注明来源:https://www.cnblogs.com/hookjc/ function saveAsExcel(tableID){ var tb = new TableToExcel(tab ...
- Eclipse导入项目java文件中文乱码
感谢大佬:https://blog.csdn.net/ordinaryprogrammerc/article/details/83013710 本文链接:https://blog.csdn.net/o ...
- js 保存并排序输入内容
转载请注明来源:https://www.cnblogs.com/hookjc/ /* Create By:jiangcheng_15 Create Date:2012-01-32 */ functio ...
- Jquery--1--选择器分类
基本选择器 $("#id") //ID选择器 $("div") //标签选择器 $(".classname ...
- 大前端工程化之写一个简单的webpack插件
今天写一个简单的webpack插件,来学习一下webpack插件 webpack插件机制可以使开发者在webpack构建过程中加入自己的行为,来针对自己项目中的一些需求做一些定制化 首先我们得知道一个 ...
- Pandas中Series与Dataframe的初始化
(一)Series初始化 1.通过列表,index自动生成 se = pd.Series(['Tom', 'Nancy', 'Jack', 'Tony']) print(se) 2.通过列表,指定in ...
- Vue 子组件更新父组件的值
今天在使用Vue中遇到了一个新的需求:子组件需要修改由父组件传递过来的值,由于子组件的值是由父组件传递过来的,不能直接修改属性的值, 我们想改变传递过来的值只能通过自定义事件的形式修改父组件的值达到修 ...