通常laravelではddを使って処理を止めたり、変数の中身を見たりしてデバッグをしていくと思います。
php
1//modelにて 2$this->favorittings()->where('microposts_id', $userId)->exists();
上記ソースddしたところ、どんなデータを取得しているのかを確認することができませんでした。
どのようにすれば中身を見ることができるでしょうか?
#追記
前後のソースを貼り付けます
php
1//View 2@if(Auth::user()->is_favoritting($micropost->id)) 3 {!! Form::open(['url' => route('favorites.destroy', ['id' => $micropost->id]), 'method' => 'delete']) !!} 4 {!! Form::submit('お気に入り解除', ['class' => 'btn btn-warning btn-xs']) !!} 5 {!! Form::close() !!} 6 7 @else 8 {!! Form::open(['url' => route('favorites.store', ['id' => $micropost->id]), 'method' => 'post']) !!} 9 {!! Form::submit('お気に入り', ['class' => 'btn btn-default btn-xs']) !!} 10 {!! Form::close() !!} 11 12 @endif
「$this->favorittings()->where('microposts_id', $userId」を
ddした結果は以下のとおりです
BelongsToMany {#238 ▼ #table: "user_microposts" #foreignPivotKey: "user_id" #relatedPivotKey: "microposts_id" #parentKey: "id" #relatedKey: "id" #relationName: "favorittings" #pivotColumns: array:2 [▶] #pivotWheres: [] #pivotWhereIns: [] #pivotValues: [] +withTimestamps: true #pivotCreatedAt: null #pivotUpdatedAt: null #using: null #accessor: "pivot" #query: Builder {#239 ▶} #parent: User {#215 ▶} #related: User {#237 ▶} }
#追記2
「dd($this->favorittings->where('microposts_id', $userId));」をした結果は以下のとおりです。
Collection {#237 ▼ #items: [] }
データは正しく取得できているんですよね?「確認することができませんでした」とありますが、何が起きましたか?できればそのdd()した箇所も含めて前後のコードを開示願います。
回答1件
あなたの回答
tips
プレビュー