简体   繁体   中英

Laravel Attempt to read property "produits" on null

whene i do the login action it return the error above where there is no user all work good

$currentTime = date("Y-m-d");
 if(Auth::check()){
  $user = Auth::user();
  $favorie = $user->favorie;
  $favprds = $favorie->produits;
}else{
 $favprds = [];
} 

this is my blade i believe that there is no probleme with blade

@for ( $i = 0; $i < count($favprds) ;$i++)
   @if ($favprds[$i]->id == $pro->id) 
   <div class="tinv-wishlist-clear">
    <a style="color:red;" href="{{ url('deleteFavorie/'.$pro->id) }}"><i class="fa fa-heart"></i></a>
   </div>
   @break
   @elseif($favprds[$i]->id != $pro->id && $i == count($favprds)-1)
<div class="tinv-wishlist-clear">
  <a href="{{ url('add-to-favorie/'.$pro->id) }}"><i class="klbth-icon-heart-1"></i></a>
</div>
@endif
@endfor
@if(count($favprds) == 0)
  <div class="tinv-wishlist-clear">
    <a style="color:#7f8c8d" href="{{ url('add-to-favorie/'.$pro->id) }}"><i class="klbth-icon-heart-1"></i></a>
  </div>
 @endif

it seems that te way i access the products is the wrong way thought i still have to test if the there's a probleme with adding to the favorite product

$currentTime = date("Y-m-d");
if ($favorie = auth()->user()->favorie ?? false) {
 $favprds = $favorie->produits;
 } else {
   $favprds = [];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM