#【创建】友情链接
DROP TABLE IF EXISTS `friend_link`;
CREATE TABLE `friend_link` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`ord` int(3) NOT NULL COMMENT '排序',
`title` varchar(50) NOT NULL COMMENT '链接名称',
`url` varchar(255) NOT NULL COMMENT '链接url',
`status` tinyint(1) NOT NULL COMMENT '链接状态 1:正常 0:下线',
`add_time` int(10) NOT NULL COMMENT '添加时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='友情链接';
#【添加】友情链接
INSERT INTO `friend_link` VALUES ('1', '0', '百度', 'www.baidu.com','1', '1585626833');
INSERT INTO `friend_link` VALUES ('2', '0', 'php中文网', 'www.php.cn','1','1585626887');
1、打开数据库管理工具phpMyAdmin
2、执行sql语句创建表并填充数据
3、查看执行结果
resources/views/admins/setting/friend_link/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">
<thead>
<tr>
<th>ID</th>
<th>友链名称</th>
<th>友链URL</th>
<th>排序</th>
<th>状态</th>
<th>添加时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
@foreach($lists as $item)
<tr>
<td>{{$item['id']}}</td>
<td>{{$item['title']}}</td>
<td>{{$item['url']}}</td>
<td>{{$item['ord']}}</td>
<td>{{$item['status']==1?'正常':'下线'}}</td>
<td>{{date('Y-m-d H:i:s',$item['add_time'])}}</td>
<td>
<button class="layui-btn layui-btn-xs" onclick="edit({{$item['id']}})">修改</button>
<button class="layui-btn layui-btn-xs layui-btn-danger" onclick="del({{$item['id']}})">删除</button>
</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>
<script type="text/javascript">
layui.use(['layer'],function(){
$ = layui.jquery;
layer = layui.layer;
});
// 添加友链
function add(){
layer.open({
type: 2,
title: '添加友链',
shade: 0.3,
area: ['450px', '450px'],
content: '/admins/setting/add_link'
});
}
// 修改友链
function edit(id){
layer.open({
type: 2,
title: '修改友链',
shade: 0.3,
area: ['450px', '450px'],
content: '/admins/setting/edit_link?id='+id
});
}
// 删除
function del(id){
layer.confirm('确定要删除吗?', {
icon:3,
btn: ['确定','取消']
}, function(){
$.get('/admins/setting/del_link',{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>
laravel/app/Http/Controllers/admins/Setting.php
<?php
namespace App\Http\Controllers\admins;
use App\Http\Controllers\Controller;
/**
* 网站设置相关
*/
class Setting extends Controller {
}
app/Http/Controllers/admins/Setting.php
use Illuminate\Support\Facades\DB;
// 友链列表
public function friend_link() {
$data['lists'] = DB::table('friend_link')->orderBy('id', 'desc')->lists();
return view('admins/setting/friend_link/index', $data);
}
//显示设置-友情链接列表
Route::get('/admins/setting/friend_link', [Setting::class, 'friend_link']);
resources/views/admins/setting/friend_link/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>
</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">友链URL</label>
<div class="layui-input-block">
<input type="text" class="layui-input" name="url">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">排序</label>
<div class="layui-input-block">
<input type="number" class="layui-input" name="ord">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">状态</label>
<div class="layui-input-block">
<input type="checkbox" lay-skin="primary" title="上线" name="status">
</div>
</div>
<div class="layui-input-block">
<button class="layui-btn" onclick="save()">保存</button>
</div>
</div>
</body>
</html>
<script type="text/javascript">
layui.use(['form','layer'],function(){
$ = layui.jquery;
form = layui.form;
layer = layui.layer;
});
// 保存
function save(){
var title = $('input[name="title"]').val();
var url = $('input[name="url"]').val();
var ord = $('input[name="ord"]').val();
var status = $('input[name="status"]').is(':checked')?1:0;
var _token = $('input[name="_token"]').val();
$.post('/admins/setting/save_link',{title:title,url:url,ord:ord,status:status,_token:_token},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>
use Illuminate\Http\Request;
// 添加友链
public function add_link() {
return view('admins/setting/friend_link/add');
}
// 保存友链
public function save_link(Request $req) {
$data['title'] = trim($req->title);
$data['url'] = trim($req->url);
$data['ord'] = (int)$req->ord;
$data['status'] = (int)$req->status;
$data['add_time'] = time();
if ($data['title'] == '') {
return json_encode(array('code' => 1, 'msg' => '友链名称不能为空'));
}
DB::table('friend_link')->insert($data);
return json_encode(array('code' => 0, 'msg' => '保存成功'));
}
routes/web.php
//添加链接 弹窗
Route::get('/admins/setting/add_link', [Setting::class, 'add_link']);
//保存链接 处理
Route::post('/admins/setting/save_link', [Setting::class, 'save_link']);
resources/views/admins/setting/friend_link/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>
</head>
<body style="padding: 10px;">
<div class="layui-form">
@csrf
<input type="hidden" name="id" 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">友链URL</label>
<div class="layui-input-block">
<input type="text" class="layui-input" name="url" value="{{$item['url']}}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">排序</label>
<div class="layui-input-block">
<input type="number" class="layui-input" name="ord" value="{{$item['ord']}}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">状态</label>
<div class="layui-input-block">
<input type="checkbox" lay-skin="primary" title="上线" name="status" {{$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(['form','layer'],function(){
$ = layui.jquery;
form = layui.form;
layer = layui.layer;
});
// 保存
function save(){
var id = $('input[name="id"]').val();
var title = $('input[name="title"]').val();
var url = $('input[name="url"]').val();
var ord = $('input[name="ord"]').val();
var status = $('input[name="status"]').is(':checked')?1:0;
var _token = $('input[name="_token"]').val();
$.post('/admins/setting/edit_link_save',{id:id,title:title,url:url,ord:ord,status:status,_token:_token},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>
app/Http/Controllers/admins/Setting.php
// 修改友链
public function edit_link(Request $req) {
$id = (int)$req->id;
$data['item'] = DB::table('friend_link')->where('id', $id)->item();
return view('admins/setting/friend_link/edit', $data);
}
// 保存修改
public function edit_link_save(Request $req) {
$id = (int)$req->id;
$data['title'] = trim($req->title);
$data['url'] = trim($req->url);
$data['ord'] = (int)$req->ord;
$data['status'] = (int)$req->status;
$data['add_time'] = time();
if ($data['title'] == '') {
return json_encode(array('code' => 1, 'msg' => '友链名称不能为空'));
}
DB::table('friend_link')->where('id', $id)->update($data);
return json_encode(array('code' => 0, 'msg' => '保存成功'));
}
routes/web.php
//编辑链接 弹窗
Route::get('/admins/setting/edit_link', [Setting::class, 'edit_link']);
//保存编辑链接 处理
Route::post('admins/setting/edit_link_save', [Setting::class, 'edit_link_save']);
app/Http/Controllers/admins/Setting.php
// 删除友链
public function del_link(Request $req) {
$id = (int)$req->id;
DB::table('friend_link')->where('id', $id)->delete();
return json_encode(array('code' => 0, 'msg' => '删除成功'));
}
routes/web.php
//删除友情链接 处理
Route::get('/admins/setting/del_link', [Setting::class, 'del_link']);