<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;

class PostController extends Controller
{

    public function index()
    {   //普通查询 n+1
        //$posts = Post::orderByDesc('created_at')->paginate(100);

        //避免n+1
        //$posts = Post::with('author')->orderByDesc('created_at')->paginate(100);

        //指定字段 -- 报错了
        $posts = Post::with('author:name')
            ->select(['id', 'title', 'user_id', 'created_at'])
            ->orderByDesc('created_at')
            ->paginate(100);
        return view('post.index', ['posts' => $posts]);
    }
}