ログインできる掲示板を作っていて、自分の投稿一覧を作ろうとしていましたらエラーになってしまいました。
何回検索しても原因がわからなかったので、ご存知の方教えて頂けますでしょうか。
エラーは以下のように表示しています。
Error
Class "App\Http\Controllers\Post" not found
http://localhost/laravel/keijiban/public/users/1
UsersControllerの以下の箇所に赤線が引かれています。
UsersController
1$posts = Post::where('user_id', $user->id)
コードは以下の通りです。
UsersController
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use App\Models\user; 7use Auth; 8 9class UsersController extends Controller 10{ 11 /** 12 * Display a listing of the resource. 13 * 14 * @return \Illuminate\Http\Response 15 */ 16 public function index() 17 { 18 // 19 } 20 21 /** 22 * Show the form for creating a new resource. 23 * 24 * @return \Illuminate\Http\Response 25 */ 26 public function create() 27 { 28 // 29 } 30 31 /** 32 * Store a newly created resource in storage. 33 * 34 * @param \Illuminate\Http\Request $request 35 * @return \Illuminate\Http\Response 36 */ 37 public function store(Request $request) 38 { 39 // 40 } 41 42 /** 43 * Display the specified resource. 44 * 45 * @param int $id 46 * @return \Illuminate\Http\Response 47 */ 48 public function show(User $user) 49 { 50 $user = User::find($user->id); 51 $posts = Post::where('user_id', $user->id) 52 ->orderBy('created_at', 'desc') 53 ->paginate(10); 54 return view('users.show', [ 55 'user_name' => $user->name, 56 'posts' => $posts, 57 ]); 58 } 59 60 /** 61 * Show the form for editing the specified resource. 62 * 63 * @param int $id 64 * @return \Illuminate\Http\Response 65 */ 66 public function edit($id) 67 { 68 // 69 } 70 71 /** 72 * Update the specified resource in storage. 73 * 74 * @param \Illuminate\Http\Request $request 75 * @param int $id 76 * @return \Illuminate\Http\Response 77 */ 78 public function update(Request $request, $id) 79 { 80 // 81 } 82 83 /** 84 * Remove the specified resource from storage. 85 * 86 * @param int $id 87 * @return \Illuminate\Http\Response 88 */ 89 public function destroy($id) 90 { 91 // 92 } 93 94} 95
home.blade
1@extends('layouts.app') 2@section('content') 3@section('keywords', '') 4@section('description', '') 5 6@if(session('message')) 7<div class="alert alert-success">{{session('message')}}</div> 8@endif 9 10{{$user->name}}さん、こんにちは! 11@foreach ($posts as $post) 12<div class="container-fluid mt-100"> 13 <div class="row"> 14 <div class="col-md-12"> 15 <div class="card mb-4"> 16 <div class="card-header"> 17 <div class="media flex-wrap w-100 align-items-center"> 18 <div class="media-body ml-3"> <a href="{{route('post.show', $post)}}">{{ $post->title }}</a> 19 <div class="text-muted small"> {{ $post->user->name }}</div> 20 </div> 21 <div class="text-muted small ml-3"> 22 <div>投稿日</div> 23 <div><strong> {{$post->created_at->format('Y.m.d H:i')}} </strong> </div> 24 </div> 25 </div> 26 </div> 27 <div class="card-body"> 28 <p> {{$post->body}} </p> 29 </div> 30 <div class="card-footer d-flex flex-wrap justify-content-between align-items-center px-0 pt-0 pb-3"> 31 <div class="px-4 pt-3"> 32 @if ($post->comments->count() >= 1) 33 <p><span class="badge badge-primary">コメント:{{ $post->comments->count() }}件</span></p> 34 @endif 35 </div> 36 <div class="px-4 pt-3"> 37 <button type="button" class="btn btn-primary"> 38 <a href="{{route('post.show', $post)}}" style="color:white;"> 39 コメントする 40 </a> 41 </button> 42 </div> 43 </div> 44 </div> 45 </div> 46</div> 47@endforeach 48@include('layouts.footer') 49@endsection
sidebar.blade
1 <div class="list-group"> 2 <a href="{{route('home')}}" 3 class="list-group-item {{url()->current()==route('home')? 'active' : ''}}"> 4 <i class="fas fa-home pr-2"></i><span>全員の投稿一覧</span> 5 </a> 6 <a href="{{route('post.create')}}" 7 class="list-group-item {{url()->current()==route('post.create')? 'active' : ''}}"> 8 <i class="fas fa-pen-nib pr-2"></i><span>新規投稿</span> 9 </a> 10 <a href="{{route('users.show', $post->user_id)}}" 11 class="list-group-item {{url()->current()==route('users.show', $post->user_id)? 'active' : ''}}"> 12 <i class="fas fa-user-edit pr-2"></i><span>{{ $user->name }}の投稿</span> 13 </a> 14</div>
show.blade
1@extends('layouts.app') 2@section('content') 3 <div class="mb-4"> 4 <p>{{ $user->name }}の投稿</p> 5 </div> 6 7 @foreach ($posts as $post) 8 <div class="card mb-4"> 9 <div class="card-header"> 10 タイトル: {{ $post->title }} 11 投稿者: <a href="{{route('users.show', $post->user_id)}}">{{ $user->name }}</a> 12 </div> 13 <div class="card-body"> 14 <p class="card-text"> 15 <li> {{ \Illuminate\Support\Str::limit($post->body, 140) }} 16 </p> 17 <a class="card-link" href="{{ route('posts.show', ['post' => $post]) }}"> 18 続きを読む 19 </a> 20 <p class="card-text"> 21 @foreach ($post->tags as $tag) 22 <a href="{{route('top', ['tag_id'=>$tag->id])}}"> 23 {{ $tag->name }} 24 </a> 25 @endforeach 26 </p> 27 28 </div> 29 <div class="card-footer"> 30 <span class="mr-2"> 31 投稿日時 {{ $post->created_at->format('Y.m.d H:i') }} 32 </span> 33 34 @if ($post->comments->count()) 35 <span class="badge badge-primary"> 36 コメント {{ $post->comments->count() }}件 37 </span> 38 @endif 39 </div> 40 @endforeach 41 @endsection
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/01/13 07:54
2022/01/13 23:34 編集
2022/01/17 16:27 編集