laravel使用教程(Laravel如何实现无限极分类)

本文目录
Laravel如何实现无限极分类
下面由Laravel教程栏目给大家介绍Laravel如何实现无限极分类,希望对需要的朋友有所帮助!
最近开发商品功能,在尝试递归和引用方式后,蓦然回首,突然发现laravel框架有更简单高效的实现方式,无限极分类最佳实践,open code与大家共享!感兴趣的Mark一下,谢谢~
表结构如下:CREATE TABLE `goods_category` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT ’主键id’,
`name` varchar(500) DEFAULT ’’ COMMENT ’分类名称’,
`pid` int(5) unsigned DEFAULT ’0’ COMMENT ’父级id’,
`level` tinyint(3) unsigned DEFAULT ’1’ COMMENT ’分类等级’,
`status` tinyint(3) unsigned DEFAULT ’0’ COMMENT ’分类状态:0-禁用,1-正常’,
`created_at` timestamp NULL DEFAULT NULL COMMENT ’创建时间’,
`updated_at` timestamp NULL DEFAULT NULL COMMENT ’更新时间’,
PRIMARY KEY (`id`) USING BTREE,
KEY `status` (`status`)) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8mb4 COMMENT=’商品分类表’;数据存储格式:
业务代码: // 模型文件
public function children() {
return $this-》hasMany(get_class($this), ’pid’ ,’id’);
}
public function allChildren() {
return $this-》children()-》with( ’allChildren’ );
}// 控制器$list = GoodsCategory::with(’allChildren’)-》first();dd($list);处理后数据:
至此,laravel框架无限极分类实现完毕,相比递归和引用实现无限极分类的两种方式,是不是简单高效很多呢,关于更多laravel特性,欢迎评论区留言探讨。
如何用laravel生成sitemap
之前用yaf和yii框架写过sitemap:思路是根据需求生成.xml文件保存到项目指定目录中。
用laravel换一个思路,生成.xml动态链接,而不是保存文件到目录
1.配置routes,生成xml访问链接
2.根据项目逻辑生成sitemap
上代码:
配置routes
//sitemap
Route::get(’/sitemap/m/{type}.xml’, ’SitemapController@siteMap’);核心代码
《?php
namespace AppHttpControllersM;
use AppHttpControllersBaseController;
use AppModelBbsArticle;
use AppModelBbsAsk;
use AppModelBbsThread;
use AppModelMainVideo;
use AppModelGarageSeriesInfoModel;
//todo 补充其他模块
use CarbonCarbon;
use IlluminateSupportFacadesCache;
class SitemapController extends BaseController
{
//todo 写一个汇总文件
public function siteMap($type)
{
$cacheKey = "site-" . $type;
//2小时缓存 保证加载速度
if (Cache::has($cacheKey)) {
$siteMap = Cache::get($cacheKey);
} else {
$siteMap = $this-》buildSiteMap($type);
Cache::add($cacheKey, $siteMap, 120);
}
return response($siteMap)
-》header(’Content-type’, ’text/xml’);
}
/**
* Build the Site Map
*/
protected function buildSiteMap($type)
{
$sitemapInfo = ;
switch ($type) {
case ’video’:
$sitemapInfo = $this-》getVideoInfo();
break;
case ’article’:
$sitemapInfo = $this-》getArticleInfo();
break;
case ’bbs’:
$sitemapInfo = $this-》getBbsInfo();
break;
case ’ask’:
$sitemapInfo = $this-》getAskInfo();
break;
case ’series’:
$sitemapInfo = $this-》getSeriesInfo();//车型库
break;
}
$lastmod = $sitemapInfo;
$xml = ;
$xml = ’《?xml version="1.0" encoding="UTF-8"?’ . ’》’;
***隐藏网址***
$xml = ’ 《url》’;
***隐藏网址***
$xml = " 《lastmod》$lastmod《/lastmod》";
$xml = ’ 《changefreq》daily《/changefreq》’;
$xml = ’ 《priority》0.8《/priority》’;
$xml = ’ 《/url》’;
foreach ($sitemapInfo as $sitemap) {
$xml = ’ 《url》’;
$xml}《/loc》";
$xml = " 《mobile:mobile type="mobile"/》";
$xml}《/lastmod》";
$xml = " 《/url》";
}
$xml = ’《/urlset》’;
return join("
", $xml);
}
/**
* Return all the posts as $url =》 $date
*/
protected function getVideoInfo()
{
$videos = Video::where(’pub_time’, ’《=’, Carbon::now())
-》where(’published’, 2)
-》where(’is_del’, 0)
-》orderBy(’id’, ’desc’)
-》pluck(’pub_time’, ’id’)
-》all();
$res = $article = ;
foreach ($videos as $id =》 $pub_time) {
$article = $id;
$article = substr($pub_time, 0, 10);
***隐藏网址***
$res = $article;
}
return $res;
}
protected function getArticleInfo()
{
$articles = Article::where(’pub_time’, ’《=’, Carbon::now())
-》where(’published’, 2)
-》where(’is_del’, 0)
-》orderBy(’id’, ’desc’)
-》pluck(’pub_time’, ’id’)
-》take(5000)
-》all();
$res = $article = ;
foreach ($articles as $id =》 $pub_time) {
$article = $id;
$article = substr($pub_time, 0, 10);
***隐藏网址***
$res = $article;
}
return $res;
}
protected function getBbsInfo()
{
$articles = Thread::where(’visible’, 1)
-》where(’is_del’, 0)
-》orderBy(’id’, ’desc’)
-》pluck(’dateline’, ’id’)
-》take(10000)
-》all();
$res = $article = ;
foreach ($articles as $id =》 $pub_time) {
$article = $id;
$article = substr($pub_time, 0, 10);
***隐藏网址***
$res = $article;
}
return $res;
}
protected function getAskInfo()
{
$articles = Ask::where(’state’, 1)
-》orderBy(’id’, ’desc’)
-》pluck(’dateline’, ’id’)
-》take(10000)
-》all();
$res = $article = ;
foreach ($articles as $id =》 $pub_time) {
$article = $id;
$article = substr($pub_time, 0, 10);
***隐藏网址***
$res = $article;
}
return $res;
}
//车型库
protected function getSeriesInfo()
{
$articles = SeriesInfoModel::where(’status’, 1)
-》where(’is_stop’, 0)
-》pluck(’name’, ’id’)
-》all();
$res = $article = ;
foreach ($articles as $id =》 $pub_time) {
$article = $id;
$article = date(’Y-m-d’, time());
***隐藏网址***
$res = $article;
}
return $res;
}
}更多laravel框架相关技术文章,请访问laravel教程栏目!

更多文章:
unlikely(not likely和unlikely含义一样吗)
2025年10月23日 20:45
kindeditor 在dialog中居中(在Iframe中使用jquery ui dialog 怎么使弹出窗口居于浏览器中间)
2025年7月27日 23:15
ASP.NET MVC 4框架揭秘:Controller类型的缓存?RxCache缓存框架
2026年6月1日 06:00
java编程工具免费(请问java编写程序除了用eclipse,还可以用什么我主要是想做web前端的,有没有什么视图化的软件啊)
2025年8月4日 12:30
memcached性能(PHP中4个加速,缓存扩展的区别和选用建议)
2025年9月8日 08:45
2 3表示什么python(python语句:print(*[1,2,3]),是什么意思)
2026年8月14日 22:30
java下载了打不开(为什么下载了java也玩不了我的世界)
2026年5月17日 05:15
timeformat函数(GetTimeFormat怎么用,各个参数尤其是最后一个有点看不懂)
2025年11月13日 04:30
sqlite3规则(sqlite3数据库 运行一段时间后 命令行操作出现:Error:unsupported file format)
2025年10月25日 22:45
extensional mentality的意思(mentality是什么意思)
2026年1月6日 13:00











