laravel5.1-第7.1章-简单blog-文章列表 laravel5.1-第7.1章-简单blog-文章列表

2023-04-10

一、创建控制器

php artisan make:controller ArticlesController

二、注册路由

在 app/Http/routes.php 文件

Route::get('/articles', 'ArticlesController@index');

三、修改方法

public function index()
{
    $articles = Article::all();
    return view('articles.index',compact('articles'));
}

最新文章的放在前面

$articles = Article::latest()->get();

四、新建视图

新建文件:resources/view/articles/index.blade.php

https://file.lulublog.cn/images/3/2023/04/E6WR2s4vc4r5MbwB62RWrG55zZr2eZ.jpg

五、查看文章列表

http://localhost:8000/articles

https://file.lulublog.cn/images/3/2023/04/dddK4uSDsmU4kyzu94uQ44XYNjk44U.jpg

六、scopePublished

修改 index 方法

public function index()
{
   $articles = Article::latest()->published()->get();
   return view('articles.index',compact('articles'));
}

Article 新增方法

public function scopePublished($query)
{
   $query->where('published_at','<=',Carbon::now());
}

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开微信扫一扫,即可进行扫码打赏哦

阅读 327