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

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

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

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

PHP

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

Q&A

解決済

1回答

629閲覧

プロパティが抜けているけどわからない

kazu100817

総合スコア2

Laravel

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

PHP

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

0グッド

0クリップ

投稿2020/07/19 10:36

前提・実現したいこと

エラーを無くしたい
ここに質問の内容を詳しく書いてください。
(例)PHP(CakePHP)で●●なシステムを作っています。
■■な機能を実装中に以下のエラーメッセージが発生しました。
動作確認した途端エラーが出ている。

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

Undefined variable: micropost

該当のソースコード

userphp

1<?php 2 3namespace App; 4 5use Illuminate\Contracts\Auth\MustVerifyEmail; 6use Illuminate\Foundation\Auth\User as Authenticatable; 7use Illuminate\Notifications\Notifiable; 8 9class User extends Authenticatable 10{ 11 use Notifiable; 12 13 /** 14 * The attributes that are mass assignable. 15 * 16 * @var array 17 */ 18 protected $fillable = [ 19 'name', 'email', 'password', 20 ]; 21 22 /** 23 * The attributes that should be hidden for arrays. 24 * 25 * @var array 26 */ 27 protected $hidden = [ 28 'password', 'remember_token', 29 ]; 30 31 /** 32 * The attributes that should be cast to native types. 33 * 34 * @var array 35 */ 36 protected $casts = [ 37 'email_verified_at' => 'datetime', 38 ]; 39 40 public function microposts() 41 { 42 return $this->hasMany(Micropost::class); 43 } 44 45 46 /** 47 * このユーザがフォロー中のユーザ。( Userモデルとの関係を定義) 48 */ 49 public function followings() 50 { 51 return $this->belongsToMany(User::class, 'user_follow', 'user_id', 'follow_id')->withTimestamps(); 52 } 53 54 /** 55 * このユーザをフォロー中のユーザ。( Userモデルとの関係を定義) 56 */ 57 public function followers() 58 { 59 return $this->belongsToMany(User::class, 'user_follow', 'follow_id', 'user_id')->withTimestamps(); 60 } 61 62 /** 63 * $userIdで指定されたユーザをフォローする。 64 * 65 * @param int $userId 66 * @return bool 67 */ 68 public function follow($userId) 69 { 70 // すでにフォローしているかの確認 71 $exist = $this->is_following($userId); 72 // 相手が自分自身かどうかの確認 73 $its_me = $this->id == $userId; 74 75 if ($exist || $its_me) { 76 // すでにフォローしていれば何もしない 77 return false; 78 } else { 79 // 未フォローであればフォローする 80 $this->followings()->attach($userId); 81 return true; 82 } 83 } 84 85 /** 86 * $userIdで指定されたユーザをアンフォローする。 87 * 88 * @param int $userId 89 * @return bool 90 */ 91 public function unfollow($userId) 92 { 93 // すでにフォローしているかの確認 94 $exist = $this->is_following($userId); 95 // 相手が自分自身かどうかの確認 96 $its_me = $this->id == $userId; 97 98 if ($exist && !$its_me) { 99 // すでにフォローしていればフォローを外す 100 $this->followings()->detach($userId); 101 return true; 102 } else { 103 // 未フォローであれば何もしない 104 return false; 105 } 106 } 107 108 /** 109 * 指定された $userIdのユーザをこのユーザがフォロー中であるか調べる。フォロー中ならtrueを返す。 110 * 111 * @param int $userId 112 * @return bool 113 */ 114 public function is_following($userId) 115 { 116 // フォロー中ユーザの中に $userIdのものが存在するか 117 return $this->followings()->where('follow_id', $userId)->exists(); 118 } 119 /** 120 * このユーザに関係するモデルの件数をロードする。 121 */ 122 public function loadRelationshipCounts() 123 { 124 $this->loadCount(['microposts', 'followings', 'followers', 'favorites']); 125 } 126 127 /** 128 * このユーザとフォロー中ユーザの投稿に絞り込む。 129 */ 130 public function feed_microposts() 131 { 132 // このユーザがフォロー中のユーザのidを取得して配列にする 133 $userIds = $this->followings()->pluck('users.id')->toArray(); 134 // このユーザのidもその配列に追加 135 $userIds[] = $this->id; 136 // それらのユーザが所有する投稿に絞り込む 137 return Micropost::whereIn('user_id', $userIds); 138 } 139 140 public function favorites() 141 { 142 return $this->belongsToMany(Micropost::class, 'user_favorite', 'user_id', 'micropost_id')->withTimestamps(); 143 } 144 145 146 public function favolite($micropostId) 147 { 148 149 $exist = $this->is_favoriting($micropostId); 150 151 $its_me = $this->id == $micropostId; 152 153 if ($exist || $its_me) { 154 155 return false; 156 } else { 157 158 $this->favoriting()->attach($micropostId); 159 return true; 160 } 161 } 162 163 public function unfavorite($micropostId) 164 { 165 166 $exist = $this->is_favoriting($micropostId); 167 168 $its_me = $this->id == $micropostId; 169 170 if ($exist && !$its_me) { 171 172 $this->favoriting()->detach($micropostId); 173 return true; 174 } else { 175 176 return false; 177 } 178 } 179 180 public function is_favoriting($micropostId) 181 { 182 return $this->favoriting()->where('micropost_id', $micropost)->exists(); 183 } 184}

試したこと

ここに問題に対して試したことを記載してください。

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

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

ベストアンサー

micropost という変数が定義されていない、といってます。
そこらへんを確認してみましょう

#どこで定義されてるんでしょうか

投稿2020/07/19 10:41

編集2020/07/19 10:42
y_waiwai

総合スコア87719

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問