namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Post; class PostsController extends Controller { // public function index(){ $posts = Post::all(); dd($posts->toArray()); // dump die return view('posts.index'); } }
うわー どうしよ、これ。困った。。
orderBy, latest()でfetchできる。
class PostsController extends Controller { // public function index(){ // $posts = Post::orderBy('created_at', 'desc')->get(); $posts = Post::latest()->get(); dd($posts->toArray()); // dump die return view('posts.index'); } }
class PostsController extends Controller { // public function index(){ // $posts = Post::orderBy('created_at', 'desc')->get(); $posts = Post::latest()->get(); // dd($posts->toArray()); // dump die // return view('posts.index', ['posts' => $posts]); return view('posts.index')->with('posts', $posts); } }
<body> <div class="container"> <h1>Blog Posts</h1> <ul> @foreach ($posts as $post) <li><a href="">{{ $post->title }}</a></li> @endforeach </ul> </div> </body>