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

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

2023-04-10 • lulubin

一、创建控制器

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?sign=70d6570a2fff9a10e42fe7762d88e2e2&t=6ace5580

五、查看文章列表

http://localhost:8000/articles

https://file.lulublog.cn/images/3/2023/04/dddK4uSDsmU4kyzu94uQ44XYNjk44U.jpg?sign=345ac5f3b4004d7b898e68df6c6a013b&t=6ace5580

六、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());
}

打赏

×

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

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

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