#【新建】文章表
DROP TABLE IF EXISTS `article`;
CREATE TABLE `article` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`cid` int(10) NOT NULL COMMENT '文章分类',
`title` varchar(255) NOT NULL COMMENT '文章标题',
`thumb` varchar(255) NOT NULL DEFAULT '' COMMENT '封面图url',
`subtitle` varchar(255) NOT NULL COMMENT '文章副标题',
`keywords` varchar(255) NOT NULL DEFAULT '' COMMENT '文章关键字',
`descs` varchar(255) NOT NULL DEFAULT '' COMMENT '文章描述',
`auth_id` int(10) NOT NULL DEFAULT '0' COMMENT '作者id',
`pv` int(10) NOT NULL DEFAULT '0' COMMENT '浏览量',
`add_time` int(10) NOT NULL DEFAULT '0' COMMENT '添加时间',
`status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态,0未发布,1发布',
PRIMARY KEY (`id`),
KEY `view_cid` (`cid`)
) ENGINE=MyISAM AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
#【插入】 文章表
INSERT INTO `article` VALUES ('8', '0', '深圳经济特区建立40周年庆祝大会隆重举行 习近平发表重要讲话 韩正出席', '/storage/2020/10/13/nYfjV0lySqNgGHELWTP8VEkIVM0DepARyYmFQhk0.jpeg', '文章副标题', 'keywords', 'desc', '1', '0', '1602591527', '1');
INSERT INTO `article` VALUES ('9', '0', '《我和我的家乡》彩蛋:群演里混了导演?这事陈凯歌、张艺谋都做过', '/storage/2020/10/13/nYfjV0lySqNgGHELWTP8VEkIVM0DepARyYmFQhk0.jpeg', '文章副标题', 'keywords', 'desc', '1', '0', '1602591586', '1');
INSERT INTO `article` VALUES ('10', '0', '他们住养老院的原因 听完大家都泪目', '/storage/2020/10/13/nYfjV0lySqNgGHELWTP8VEkIVM0DepARyYmFQhk0.jpeg', '文章副标题', 'keywords', 'desc', '1', '0', '1602591694', '1');
#【新建】 文章详情表
DROP TABLE IF EXISTS `article_detail`;
CREATE TABLE `article_detail` (
`aid` int(10) NOT NULL COMMENT '文章id',
`contents` text NOT NULL COMMENT '文章内容',
PRIMARY KEY (`aid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
#【插入】文章详情表
INSERT INTO `article_detail` VALUES ('1', '<p><strong>asdfa<span style=\"color: rgb(0, 176, 80);\">sdfa</span>sdf</strong></p><p><strong><img src=\"/ueditor/php/upload/image/20200618/1592485297674493.png\" title=\"1592485297674493.png\" alt=\"image.png\"/></strong></p>');
INSERT INTO `article_detail` VALUES ('2', '<p><span style=\"background-color: rgb(192, 80, 77); color: rgb(127, 127, 127);\"><span style=\"background-color: rgb(192, 80, 77); font-family: "Helvetica Neue", Helvetica, "PingFang SC", 微软雅黑, Tahoma, Arial, sans-serif; font-size: 14px;\">对于刚接触web编程的同学来说,不知道哪个教程比较好,或者适合自己,php中文网今天就为同学们精选了5个html入门视频教程,全部是免费的课程,非常适合零基础学员自学,欢迎学习!</span><br/><br/><span style=\"background-color: rgb(192, 80, 77); font-family: "Helvetica Neue", Helvetica, "PingFang SC", 微软雅黑, Tahoma, Arial, sans-serif; font-size: 14px;\">注:html入门视频教程不提供百度云等下载,直接在线观看即可,非常方便!另外课程是按照时间倒叙排列的,无排名之分,同学们喜欢就好!</span></span><br/><br/></p>');
INSERT INTO `article_detail` VALUES ('3', '<p><span style="background-color: rgb(192, 80, 77); color: rgb(127, 127, 127);"><span style="background-color: rgb(192, 80, 77); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, 微软雅黑, Tahoma, Arial, sans-serif; font-size: 14px;">对于刚接触web编程的同学来说,不知道哪个教程比较好,或者适合自己,php中文网今天就为同学们精选了5个html入门视频教程,全部是免费的课程,非常适合零基础学员自学,欢迎学习!</span><br/><br/><span style="background-color: rgb(192, 80, 77); font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;PingFang SC&quot;, 微软雅黑, Tahoma, Arial, sans-serif; font-size: 14px;">注:html入门视频教程不提供百度云等下载,直接在线观看即可,非常方便!另外课程是按照时间倒叙排列的,无排名之分,同学们喜欢就好!</span></span><br/><br/></p>');
resources/views/admins/article/index.blade.php
<!DOCTYPE html>
<html>
<head>
<title>文章列表</title>
<link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
<script type="text/javascript" src="/static/layui/layui.js"></script>
</head>
<body style="padding: 10px;">
<div style="text-align: right;">
<button class="layui-btn layui-btn-sm" onclick="add()"><i class="layui-icon layui-icon-addition"></i>添加</button>
</div>
<table class="layui-table" style="min-width: 800px">
<thead>
<tr>
<th>ID</th>
<th>文章分类</th>
<th>文章标题</th>
<th>文章缩略图</th>
<th>文章作者</th>
<th>PV</th>
<th>状态</th>
<th>添加时间</th>
<th style="width: 90px">操作</th>
</tr>
</thead>
<tbody>
@foreach($data as $val)
<tr>
<td>{{$val['id']}}</td>
<td>{{isset($cates[$val['cid']])?$cates[$val['cid']]['title']:''}}</td>
<td style="width: 90px">{{$val['title']}}</td>
<td><img src="{{$val['thumb']}}" style="height: 30px;"></td>
<td>{{isset($auths[$val['auth_id']])?$auths[$val['auth_id']]['username']:''}}</td>
<td>{{$val['pv']}}</td>
<td>{{$val['status']==0?'草稿':'已发布'}}</td>
<td>{{date('Y-m-d H:i:s',$val['add_time'])}}</td>
<td style="width: 90px">
<button class="layui-btn layui-btn-xs" onclick="edit({{$val['id']}})">修改</button>
<button class="layui-btn layui-btn-xs layui-btn-danger" onclick="del({{$val['id']}})">删除</button>
</td>
</tr>
@endforeach
</tbody>
</table>
<div id="pages" style="text-align: center"></div>
</body>
</html>
<script type="text/javascript">
layui.use(['layer', 'laypage'], function () {
$ = layui.jquery;
layer = layui.layer;
var laypage = layui.laypage;
laypage.render({
elem: 'pages'
, curr:{{$page}}
, limit:{{$pageSize}}
, count: {{$total}}
, jump: function (obj, first) {
//首次不执行
if (!first) {
window.location.href = '/admins/article/index?page=' + obj.curr;
}
}
});
});
// 添加文章
function add() {
layer.open({
type: 2,
title: '添加文章',
shade: 0.3,
area: ['750px', '750px'],
content: '/admins/article/add'
});
}
// 修改文章
function edit(id) {
layer.open({
type: 2,
title: '修改文章',
shade: 0.3,
area: ['750px', '750px'],
content: '/admins/article/edit?id=' + id
});
}
// 删除
function del(id) {
layer.confirm('确定要删除吗?', {
icon: 3,
btn: ['确定', '取消']
}, function () {
$.get('/admins/article/del', {id: id}, function (res) {
if (res.code > 0) {
return layer.alert(res.msg, {icon: 2});
}
layer.msg(res.msg);
setTimeout(function () {
window.location.reload();
}, 1000);
}, 'json');
});
}
</script>
app/Http/Controllers/admins/Article.php
// 文章列表
public function index(Request $req) {
$data['page'] = max(1, (int)$req->page);
$data['pageSize'] = 10;
$pageObj = DB::table('article')->orderBy('id', 'desc')->paginate($data['pageSize']);
$data['total'] = $pageObj->total();
$data['data'] = $pageObj->items();
$data['data'] = array_map(function ($val) { return (array)$val; }, $data['data']);
// 文章分类
$data['cates'] = DB::table('article_cate')->cates('id');
// 文章作者
$data['auths'] = DB::table('admin')->cates('id');
return view('/admins/article/index', $data);
}
//显示文章列表
Route::get('/admins/article/index', [Article::class, 'index']);
app/Http/Controllers/admins/Files.php
<?php
namespace App\Http\Controllers\admins;
use App\Http\Controllers\Controller;
/**
* 上传文件 控制器
* @package App\Http\Controllers\admins
*/
class Files extends Controller {
}
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
// 图片上传-中间不要随意向前端传递数据,会导致请求上传接口出现异常问题
public function uploadimg(Request $request) {
$path = $request->file('file')->store('public/article/' . date('Y/m/d'));
$url = Storage::url($path);
// 把路径保存到数据库中,同时加状态,初始状态为0(表示草稿状态),1(表示正常使用状态)
return json_encode(array('code' => 0, 'msg' => '', 'data' => array('src' => $url)));
}
参考:Laravel 图片不显示解决方案
1、在项目目录下执行命令创建软链接
php artisan storage:link
// 文件上传相关
Route::post('/admins/files/uploadimg',[Files::class, 'uploadimg']);
resources/views/admins/article/add.blade.php
<!DOCTYPE html>
<html>
<head>
<title>添加文章</title>
<link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
<script type="text/javascript" src="/static/layui/layui.js"></script>
<script type="text/javascript" src="/static/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="/static/ueditor/ueditor.all.js"></script>
</head>
<body style="padding: 10px;">
<div class="layui-form">
@csrf
<div class="layui-form-item">
<label class="layui-form-label">文章标题</label>
<div class="layui-input-block">
<input type="text" class="layui-input" name="title">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">文章副标题</label>
<div class="layui-input-block">
<input type="text" class="layui-input" name="subtitle">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">文章分类</label>
<div class="layui-input-block">
<select name="cid">
@foreach($cates as $val)
<option value="{{$val['id']}}">{{$val['title']}}</option>
@endforeach
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">缩略图</label>
<div class="layui-input-block">
<button class="layui-btn" id="uploadimg"><i class="layui-icon"></i>上传图片</button>
<img id="preimg" src="" style="height: 30px;">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">关键字</label>
<div class="layui-input-block">
<input type="text" class="layui-input" name="keywords">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">描述</label>
<div class="layui-input-block">
<input type="text" class="layui-input" name="descs">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">文章内容</label>
<div class="layui-input-block">
<script id="container" name="content" type="text/plain"></script>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">状态</label>
<div class="layui-input-block">
<input type="checkbox" name="status" lay-skin="primary" title="发布">
</div>
</div>
<div class="layui-input-block">
<button class="layui-btn" onclick="save()">保存</button>
</div>
</div>
</body>
</html>
<script type="text/javascript">
layui.use(['upload','form','layer'],function(){
$ = layui.jquery;
layer = layui.layer;
form = layui.form;
var upload = layui.upload;
upload.render({
elem: '#uploadimg'
,url: '/admins/files/uploadimg'
,data: {
_token: function(){
return $('input[name="_token"]').val();
}
}
,done: function(res){
console.log("图片上传2:",res);
if(res.code>0){
return layer.alert(res.msg,{icon:2});
}
$('#preimg').attr('src',res.data.src);
}
});
});
// 保存
function save(){
var data = {};
data.title = $('input[name="title"]').val();
data.subtitle = $('input[name="subtitle"]').val();
data.cid = $('select[name="cid"]').val();
data.thumb = $('#preimg').attr('src');
data.keywords = $('input[name="keywords"]').val();
data.descs = $('input[name="descs"]').val();
data.contents = ue.getContent();
data.status = $('input[name="status"]').is(':checked')?1:0;
data._token = $('input[name="_token"]').val();
if(data.title==''){
return layer.alert('请输入文章标题',{icon:2});
}
$.post('/admins/article/save',data,function(res){
if(res.code>0){
return layer.alert(res.msg,{icon:2});
}
layer.msg(res.msg);
setTimeout(function(){parent.window.location.reload();},1000);
},'json');
}
</script>
<script type="text/javascript">
var ue = UE.getEditor('container',{initialFrameWidth:'100%',initialFrameHeight:500,zIndex:1});
</script>
// 添加文章
public function add() {
$data['cates'] = DB::table('article_cate')->lists();
return view('/admins/article/add', $data);
}
// 保存文章
public function save(Request $req) {
$data['title'] = trim($req->title);
$data['subtitle'] = trim($req->subtitle);
$data['cid'] = (int)$req->cid;
$data['thumb'] = trim($req->thumb);
$data['keywords'] = trim($req->keywords);
$data['descs'] = trim($req->descs);
$data['auth_id'] = $req->admin->id;
$data['status'] = (int)$req->status;
$data['add_time'] = time();
$contents = trim($req->contents);
$code = 1;
$msg = '保存失败';
// 数据写入数据库(持久化)
$aid = DB::table('article')->insertGetId($data);
if ($aid) {
$res = DB::table('article_detail')->insert(['aid' => $aid, 'contents' => $contents]);
if ($res) {
$code = 0;
$msg = '保存成功';
}
}
return json_encode(array('code' => $code, 'msg' => $msg));
}
//添加文章弹窗
Route::get('/admins/article/add',[Article::class, 'add']);
//保存文章处理
Route::post('/admins/article/save',[Article::class, 'save']);
resources/views/admins/article/edit.blade.php
<!DOCTYPE html>
<html>
<head>
<title>修改文章</title>
<link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
<script type="text/javascript" src="/static/layui/layui.js"></script>
<script type="text/javascript" src="/static/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="/static/ueditor/ueditor.all.js"></script>
</head>
<body style="padding: 10px;">
<div class="layui-form">
@csrf
<input type="hidden" name="aid" value="{{$item['id']}}">
<div class="layui-form-item">
<label class="layui-form-label">文章标题</label>
<div class="layui-input-block">
<input type="text" class="layui-input" name="title" value="{{$item['title']}}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">文章副标题</label>
<div class="layui-input-block">
<input type="text" class="layui-input" name="subtitle" value="{{$item['subtitle']}}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">文章分类</label>
<div class="layui-input-block">
<select name="cid">
@foreach($cates as $val)
<option value="{{$val['id']}}" {{$item['cid']==$val['id']?'selected':''}}>{{$val['title']}}</option>
@endforeach
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">缩略图</label>
<div class="layui-input-block">
<button class="layui-btn" id="uploadimg"><i class="layui-icon"></i>上传图片</button>
<img id="preimg" src="{{$item['thumb']}}" style="height: 30px;">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">关键字</label>
<div class="layui-input-block">
<input type="text" class="layui-input" name="keywords" value="{{$item['keywords']}}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">描述</label>
<div class="layui-input-block">
<input type="text" class="layui-input" name="descs" value="{{$item['descs']}}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">文章内容</label>
<div class="layui-input-block">
<script id="container" name="content" type="text/plain">{!!$detail['contents']!!}</script>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">状态</label>
<div class="layui-input-block">
<input type="checkbox" name="status" lay-skin="primary" title="发布" {{$item['status']==1?'checked':''}}>
</div>
</div>
<div class="layui-input-block">
<button class="layui-btn" onclick="save()">保存</button>
</div>
</div>
</body>
</html>
<script type="text/javascript">
layui.use(['upload','form','layer'],function(){
$ = layui.jquery;
layer = layui.layer;
form = layui.form;
var upload = layui.upload;
upload.render({
elem: '#uploadimg'
,url: '/admins/files/uploadimg'
,data: {
_token: function(){
return $('input[name="_token"]').val();
}
}
,done: function(res){
$('#preimg').attr('src',res.data.src);
}
});
});
// 保存
function save(){
var data = {};
data.aid = $('input[name="aid"]').val();
data.title = $('input[name="title"]').val();
data.subtitle = $('input[name="subtitle"]').val();
data.cid = $('select[name="cid"]').val();
data.thumb = $('#preimg').attr('src');
data.keywords = $('input[name="keywords"]').val();
data.descs = $('input[name="descs"]').val();
data.contents = ue.getContent();
data.status = $('input[name="status"]').is(':checked')?1:0;
data._token = $('input[name="_token"]').val();
if(data.title==''){
return layer.alert('请输入文章标题',{icon:2});
}
console.log("保存文章编辑")
$.post('/admins/article/save_edit',data,function(res){
if(res.code>0){
return layer.alert(res.msg,{icon:2});
}
layer.msg(res.msg);
setTimeout(function(){parent.window.location.reload();},1000);
},'json');
}
</script>
<script type="text/javascript">
var ue = UE.getEditor('container',{initialFrameWidth:'100%',initialFrameHeight:500,zIndex:1});
</script>
app/Http/Controllers/admins/Article.php
// 修改文章
public function edit(Request $req) {
$id = (int)$req->id;
$data['item'] = DB::table('article')->where('id', $id)->item();
$data['detail'] = DB::table('article_detail')->where('aid', $id)->item();
$data['cates'] = DB::table('article_cate')->lists();
return view('admins/article/edit', $data);
}
// 保存修改
public function save_edit(Request $req) {
$aid = (int)$req->aid;
$data['title'] = trim($req->title);
$data['subtitle'] = trim($req->subtitle);
$data['cid'] = (int)$req->cid;
$data['thumb'] = trim($req->thumb);
$data['keywords'] = trim($req->keywords);
$data['descs'] = trim($req->descs);
$data['auth_id'] = $req->admin->id;
$data['status'] = (int)$req->status;
$data['add_time'] = time();
$this->contents = trim($req->contents);
$this->aid = $aid;
$this->data = $data;
// 数据写入数据库(持久化)
DB::transaction(function () {
DB::table('article')->where('id', $this->aid)->update($this->data);
DB::table('article_detail')->where('aid', $this->aid)->update(['contents' => $this->contents]);
});
return json_encode(array('code' => 0, 'msg' => '保存成功'));
}
routes/web.php
//编辑文章弹窗
Route::get('/admins/article/edit',[Article::class, 'edit']);
//保存编辑文章处理
Route::post('/admins/article/save_edit',[Article::class, 'save_edit']);
// 删除文章
public function del(Request $req) {
$aid = (int)$req->id;
DB::table('article')->where('id', $aid)->delete();
DB::table('article_detail')->where('aid', $aid)->delete();
return json_encode(array('code' => 0, 'msg' => '删除成功'));
}
//删除文章处理
Route::get('/admins/article/del',[Article::class, 'del']);