前提・実現したいこと
Laravel5.5でログインしたユーザーにお気に入りボタンを表示する機能を開発しております。
show.blade.phpにfavolite_botton.blade.phpを読み込んで表示させたいと思っております。
そこでAuthチェックし、 @if (($user)->is_favoliting)のところでUser.phpにfavolitingが記載されているのにも関わらずエラーが出てしまいます。
ご助力のほどよろしくお願い申し上げます。
発生している問題・エラーメッセージ
Undefined property: Illuminate\Auth\SessionGuard::$is_favoliting (favolite_botton.blade.php)
該当のソースコード
PHP
1@if (Auth::check()) 2 @if (($user)->is_favoliting) 3 {!! Form::open(['route' => ['user.unfavolite', $user->id], 'method' => 'delete']) !!} 4 {!! Form::submit('Unfavolite', ['class' => "btn btn-danger btn-block"]) !!} 5 {!! Form::close() !!} 6 @else 7 {!! Form::open(['route' => ['user.favolite', $user->id]]) !!} 8 {!! Form::submit('Favolite', ['class' => "btn btn-primary btn-block"]) !!} 9 {!! Form::close() !!} 10 @endif 11@else 12 ログインなし 13@endif
User.php
PHP
1public function favolites() 2 { 3 return $this->belongsToMany(User::class, 'favolites', 'user_id', 'school_id')->withTimestamps(); 4 } 5 6 public function favolite($userId) { 7 8$exist = $this->is_favoliting($userId); 9 10$its_me = $this->id == $userId; 11 12 if ($exist || $its_me) { 13 14 return false; 15 16 } else { 17 18 19 $this->favolitings()->attach($userId); 20 21 return true; 22 23 } 24} 25 26public function unfavolite($userId) 27{ 28 $exist = $this->is_favoliting($userId); 29 // 自分自身ではないかの確認 30 $its_me = $this->id == $userId; 31 32 if ($exist && !$its_me) { 33 $this->favolitings()->detach($userId); 34 return true; 35 } else { 36 return false; 37 } 38} 39 40public function favoliting ($userId) { 41 return $this->favolitings()->where('favolite_id', $userId)->exists(); 42}
試したこと
@if後にdd($user)を挿入
SessionGuard {#264 ▼ #name: "user" #lastAttempted: null #viaRemember: false #session: Store {#244 ▶} #cookie: CookieJar {#242 ▶} #request: Request {#42 ▶} #events: Dispatcher {#26 ▶} #loggedOut: false #recallAttempted: false #user: User {#274 ▼ #fillable: array:3 [▶] #hidden: array:2 [▶] #connection: "mysql" #table: null #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:7 [▼ "id" => 1 "name" => "user2" "email" => "user2@user.com" "password" => "$2y$10$yo4zNCkH2M.VJgAayNTnHehSAxCjqHiA/hvmb3IfC.QcnTHOueoOy" "remember_token" => null "created_at" => "2018-04-10 14:41:32" "updated_at" => "2018-04-10 14:41:32" ] #original: array:7 [▶] #changes: [] #casts: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: [] #touches: [] +timestamps: true #visible: [] #guarded: array:1 [▶] #rememberTokenName: "remember_token"``` ### 補足情報(FW/ツールのバージョンなど) ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/13 08:33