《laravel大型项目系列教程(四)》由会员分享,可在线阅读,更多相关《laravel大型项目系列教程(四)(12页珍藏版)》请在金锄头文库上搜索。
1、Laravel大型项目系列教程(四)一、前言上节教程中实现了发布文章的功能,本节教程中将大概实现在首页和用户主页分页显示文章和标签列表、用户能够修改删除文章。二、Lets go1.首页显示文章和标签列表我们需要在首页显示文章和标签列表,修改views/index.blade.php:extends(_layouts.default)section(main) foreach ($articles as $article) id) $article-title by $article-user-nickname posted on $article-created_at-format(Y/m/d
2、 H:i) under foreach ($article-tags as $tag) $tag-name endforeach if ($article-summary) $article-summary endif endforeach Tags for ($i = 0, $len = count($tags); $i $len; $i+) $tags$i-name if ($i = 0) $tags$i-count elseif ($i = 1) $tags$i-count elseif ($i = 2) $tags$i-count else $tags$i-count endif en
3、dfor stop在custom.css中增加:media only screen and (min-width: 641px) .blog-sidebar font-size: 1.4rem; .blog-main padding: 20px 0;.blog-title margin: 10px 0 20px 0;.blog-meta font-size: 14px; margin: 10px 0 20px 0; color: #222;.blog-meta a color: #27ae60;修改routes.php:Route:get(/, function() $articles = A
4、rticle:with(user, tags)-orderBy(created_at, desc)-paginate(10); $tags = Tag:where(count, , 0)-orderBy(count, desc)-orderBy(updated_at, desc)-take(10)-get(); return View:make(index)-with(articles, $articles)-with(tags, $tags););上面Article:with()使用了预加载,可以减少查询次数。发布几篇文章然后访问首页:2.实现用户主页我们在发表文章后应该能在用户主页看到文章
5、列表,并能对文章进行操作,先在导航栏nav.blade.php的else上面添加一个按钮My Articles: My Articles修改home.blade.php:extends(_layouts.default)section(main) Title Tags if ($user-id = Auth:id() Managment endif foreach ($articles as $article) id) $article-title foreach ($article-tags as $tag) $tag-name endforeach if ($user-id = Auth:
6、id() id . /edit) class=am-btn am-btn-xs am-btn-primary Edit Form:open(array(url = article/ . $article-id, method = DELETE, style = display: inline;) id Delete Form:close() endif endforeach No Yes $(function() $(id=delete).on(click, function() $(.am-modal-bd).text(Sure you want to delete it?); $(#my-confirm).mo