Laravel 控制器 controller

创建控制器文件

1
php artisan make:controller Post

./app/Http/Controllers/ 目录下生成 PostController.php

1
2
3
4
5
6
use \App\Post;

class PostController extends Controller
{

}

获取数据列表

1
2
3
4
5
6
7
8
9
10
11
use \App\Post;

class PostController extends Controller
{
// 列表
public function index()
{
$posts = Post::orderBy('created_at', 'desc')->get();
return view('post/index', compact('posts'));
}
}

数据分页

1
2
3
4
5
6
7
8
9
class PostController extends Controller
{
// 列表
public function index()
{
$posts = Post::orderBy('created_at', 'desc')->paginate(6);
return view('post/index', compact('posts'));
}
}

模版文件

1
{{$posts->links}}

预加载模型

1
2
= ...->with('关联模型')->...;
$posts->load('user');