質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

0回答

724閲覧

twitterクローンのユーザー一覧表示

kazu100817

総合スコア2

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2020/07/19 09:28

編集2020/07/19 09:58

前提・実現したいこと

ユーザーの一覧を表示する時にエラーが出てしまいます。
ここに質問の内容を詳しく書いてください。
PHPでTwitterクローンを本を読みながら作っているのですが本のエラーまとめに対して乗っていないので質問させていただきました。。
■■な機能を実装中に以下のエラーメッセージが発生しました。
Twitterのユーザー一覧を表示させようとすると下記の様なエラーが出ます。

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

Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View: /home/ubuntu/environment/microposts/resources/views/user_favorite/favorite_button.blade.php)

該当のソースコード

リンク内容
User.php

<?php namespace App; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; public function microposts() { return $this->hasMany(Micropost::class); } /** * このユーザがフォロー中のユーザ。( Userモデルとの関係を定義) */ public function followings() { return $this->belongsToMany(User::class, 'user_follow', 'user_id', 'follow_id')->withTimestamps(); } /** * このユーザをフォロー中のユーザ。( Userモデルとの関係を定義) */ public function followers() { return $this->belongsToMany(User::class, 'user_follow', 'follow_id', 'user_id')->withTimestamps(); } /** * $userIdで指定されたユーザをフォローする。 * * @param int $userId * @return bool */ public function follow($userId) { // すでにフォローしているかの確認 $exist = $this->is_following($userId); // 相手が自分自身かどうかの確認 $its_me = $this->id == $userId; if ($exist || $its_me) { // すでにフォローしていれば何もしない return false; } else { // 未フォローであればフォローする $this->followings()->attach($userId); return true; } } /** * $userIdで指定されたユーザをアンフォローする。 * * @param int $userId * @return bool */ public function unfollow($userId) { // すでにフォローしているかの確認 $exist = $this->is_following($userId); // 相手が自分自身かどうかの確認 $its_me = $this->id == $userId; if ($exist && !$its_me) { // すでにフォローしていればフォローを外す $this->followings()->detach($userId); return true; } else { // 未フォローであれば何もしない return false; } } /** * 指定された $userIdのユーザをこのユーザがフォロー中であるか調べる。フォロー中ならtrueを返す。 * * @param int $userId * @return bool */ public function is_following($userId) { // フォロー中ユーザの中に $userIdのものが存在するか return $this->followings()->where('follow_id', $userId)->exists(); } /** * このユーザに関係するモデルの件数をロードする。 */ public function loadRelationshipCounts() { $this->loadCount(['microposts', 'followings', 'followers', 'favorites']); } /** * このユーザとフォロー中ユーザの投稿に絞り込む。 */ public function feed_microposts() { // このユーザがフォロー中のユーザのidを取得して配列にする $userIds = $this->followings()->pluck('users.id')->toArray(); // このユーザのidもその配列に追加 $userIds[] = $this->id; // それらのユーザが所有する投稿に絞り込む return Micropost::whereIn('user_id', $userIds); } public function favorites() { return $this->belongsToMany(Micropost::class, 'user_favorite', 'user_id', 'micropost_id')->withTimestamps(); } public function favorite($usetId) { $exist = $this->is_favoriting($userId); $its_me = $this->id == $userId; if ($exist || $its_me) { return false; } else { $this->favoriting()->attach($userId); return true; } } public function unfavorite($userId) { $exist = $this->is_favoriting($userId); $its_me = $this->id == $userId; if ($exist && !$its_me) { $this->favoriting()->detach($userId); return true; } else { return false; } } public function is_favoriting($micropostId) { return $this->favoriting()->where('micropost_id', $micropostId)->exists(); } }

UsersController.php

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\User; use Appp\Micropost; class UsersController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { // ユーザ一覧をidの降順で取得 $users = User::orderBy('id', 'desc')->paginate(10); // ユーザ一覧ビューでそれを表示 return view('users.index', [ 'users' => $users, ]); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { // } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { // idの値でユーザを検索して取得 $user = User::findOrFail($id); // 関係するモデルの件数をロード $user->loadRelationshipCounts(); // ユーザの投稿一覧を作成日時の降順で取得 $microposts = $user->microposts()->orderBy('created_at', 'desc')->paginate(10); // ユーザ詳細ビューでそれらを表示 return view('users.show', [ 'user' => $user, 'microposts' => $microposts, ]); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { // } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { // } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $micropost = \App\Micropost::findOrFail($id); // 認証済みユーザ(閲覧者)がその投稿の所有者である場合は、投稿を削除 if (\Auth::id() === $micropost->user_id) { $micropost->delete(); } // 前のURLへリダイレクトさせる return back(); } /** * ユーザのフォロー一覧ページを表示するアクション。 * * @param $id ユーザのid * @return \Illuminate\Http\Response */ public function followings($id) { // idの値でユーザを検索して取得 $user = User::findOrFail($id); // 関係するモデルの件数をロード $user->loadRelationshipCounts(); // ユーザのフォロー一覧を取得 $followings = $user->followings()->paginate(10); // フォロー一覧ビューでそれらを表示 return view('users.followings', [ 'user' => $user, 'users' => $followings, ]); } /** * ユーザのフォロワー一覧ページを表示するアクション。 * * @param $id ユーザのid * @return \Illuminate\Http\Response */ public function followers($id) { // idの値でユーザを検索して取得 $user = User::findOrFail($id); // 関係するモデルの件数をロード $user->loadRelationshipCounts(); // ユーザのフォロワー一覧を取得 $followers = $user->followers()->paginate(10); // フォロワー一覧ビューでそれらを表示 return view('users.followers', [ 'user' => $user, 'users' => $followers, ]); } public function favorites($id) { $user = User::findOrFail($id); $user->loadRelationshipCounts(); $favorites = $user->favorites()->paginate(10); return view('users.favorites', [ 'user' => $user, 'users' => $favorites, ]); } }

補足情報(FW/ツールのバージョンなど)

使っているクラウドはCloud9です。
ここにより詳細な情報を記載してください。
エラーが出た文は本に書いてあったのを見て書きました下記に記載します。

@if (Auth::id() != $user->id) @if (Auth::user()->is_following($user->id)) {{-- アンフォローボタンのフォーム --}} {!! Form::open(['route' => ['user.unfollow', $user->id], 'method' => 'delete']) !!} {!! Form::submit('Unfollow', ['class' => "btn btn-danger btn-block"]) !!} {!! Form::close() !!} @else {{-- フォローボタンのフォーム --}} {!! Form::open(['route' => ['user.follow', $user->id]]) !!} {!! Form::submit('Follow', ['class' => "btn btn-primary btn-block"]) !!} {!! Form::close() !!} @endif @endif

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2020/07/19 09:50

「Laravel」はタグにあった方が良いかと。 ただエラー自体はPHPのクラスのところの基本的な内容ですね。
kazu100817

2020/07/19 10:01

返信ありがとうございますLaravelを追加しました。
posaune

2020/07/21 10:55

最後の補足情報のコードの前後が重要なのですがわかりますか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問