###カテゴリーごとをまとめたの投稿表示
ここに実現したいことを箇条書きで書いてください。
- indexファイルから国名を選択する
- [ ]選択された国のブログのみをpostファイルに表示
前提
Laravelを使って、国名を押すとその国のブログが見れるアプリを作っています。indexファイルから国名を選択し、postファイルにその国の投稿のみを表示させようとしています。
発生している問題・エラーメッセージ
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$country
該当のソースコード
↓index.blade.php
php
1<!DOCTYPE html> 2<html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> 3 <head> 4 <meta charset="utf-8"> 5 <title>Traveler</title> 6 <!-- Fonts --> 7 <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet"> 8 </head> 9 <body> 10 <h1>Choose Country</h1> 11 <div class='countries'> 12 @foreach ($countries as $country) 13 <div class='country'> 14 <h2 class='country_name'> 15 <a href="/countries/{{ $country->id }}">{{ $country->country_name }}</a> 16 </h2> 17 </div> 18 @endforeach 19 </div> 20 </body> 21</html>
↓post.blade.php
php
1<!DOCTYPE html> 2<html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> 3 <head> 4 <meta charset="utf-8"> 5 <title>Traveler</title> 6 <!-- Fonts --> 7 <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet"> 8 </head> 9 <body> 10 <h1>{{$posts->country->country_name}}</h1> 11 <div class='posts'> 12 @foreach ($posts as $post) 13 <div class='post'> 14 <h2 class='title'>{{ $post->title }}</h2> 15 <p class='body'>{{ $post->body }}</p> 16 <p class='category'>{{ $post->category->category_name }}</p> 17 </div> 18 @endforeach 19 </div> 20 <div class='paginate'>{{$posts->links}}</div> 21 </body> 22</html>
↓CountryController
php
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use App\Models\Country; 7 8class CountryController extends Controller 9{ 10 public function index(Country $country) 11 { 12 return view('posts/index')->with(['countries' => $country->get()]); 13 } 14 15 public function post(Country $country) 16 { 17 return view('countries/post')->with(['posts' => $country->getByCountry()]); 18 } 19}
↓PostController
php
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use App\Models\Post; 7 8class PostController extends Controller 9{ 10 // public function index(Post $post) 11 // { 12 // return view('posts/index')->with(['posts' => $post->get()]); 13 // } 14} 15?>
↓web.php
php
1<?php 2 3use Illuminate\Support\Facades\Route; 4use App\Http\Controllers\PostController; 5use App\Http\Controllers\CountryController; 6 7Route::get('/', [CountryController::class, 'index']); 8Route::get('/countries/{country}', [CountryController::class,'post']); 9
試したこと
postsとccountriesテーブルのリレーションを確認しました。
補足情報(FW/ツールのバージョンなど)
PHP 8.0.26

回答2件
あなたの回答
tips
プレビュー