Laravel SNS投稿表示

前提・実現したいこと

SNS投稿表示(下記のコードの$timelineを表示)を実装させたいです。
phpMyAdmin に投稿は保存できていることは確認出来ています。

発生している問題・エラーメッセージ

Trying to get property 'username' of non-object

該当のソースコード

index.blade.php

@foreach ($timelines as $timeline) {{ $timeline->user->username }} {{ $timeline->post }} {{ $timeline->created_at->format('Y-m-d H:i') }} @endforeach

PostsController.php

public function index(User $user, Follow $follow, Post $post) { $follow_count = $follow->getFollowCount(Auth::id()); $follower_count = $follow->getFollowerCount(Auth::id()); $followList = $follow->where('follower', Auth::id()) ->pluck('follow') ->toArray(); $timelines = $post->getTimelines(Auth::id(), $followList); return view('posts.index', [ 'follow_count' => $follow_count, 'follower_count' => $follower_count, 'timelines' => $timelines, ]); }

Post.php

public function getTimeLines(Int $user_id, array $follow) { $follow[] = $user_id; return $this->whereIn('user_id', $follow)->orderBy('created_at', 'DESC')->paginate(); }

users_table.php

public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id')->autoIncrement(); $table->string('username',255); $table->string('mail',255); $table->string('password',255); $table->string('bio',400)->nullable(); $table->string('images',255)->default('dawn.png')->nullable(); $table->timestamps(); }); }

follows_table.php

public function up() { Schema::create('follows', function (Blueprint $table) { $table->increments('id')->autoIncrement(); $table->integer('follow'); $table->integer('follower'); $table->timestamp('created_at')->useCurrent(); }); }

バージョン

laravel 6.20.43
php 7.4.21
homestead

ご教授の程、よろしくお願い致します。

コメントを投稿

0 コメント