skip to Main Content

Controller

 $userAlbums =album::select('album_id')->where('created_by','=',Auth::id())->count();
 $userData = user::where('id','=',Auth::id())->first();
 return view('welcome', compact('userData','userAlbums'));
    

Blade

@if($userData->email_varified_at == null)
@include('include.email-verification')
@endif

if i try to display $userData->email_varified_at variable it works ,but the IF condition also run every time even if email_varified_at field is not null.

is this have any thing to do with casting of filed?

2

Answers


  1. have you tried the empty function?

    @if( !empty($str->a))
    ... // do somethin
    @endif
    

    The empty() function checks whether a variable is empty or not.

    This function returns false if the variable exists and is not empty, otherwise it returns true.

    The following values evaluate to empty:

    0

    0.0

    "0"

    ""

    NULL

    FALSE

    array()

    Login or Signup to reply.
  2. The email_verified_at field of a User model in Laravel is either a datetime string or null. When you use the if statement in Blade to check if the field is null, you need to use is_null() or !isset() instead of empty(), which only works with variables that are set but have no value (e.g., "", 0, false, null, [], etc.) but not with variables that are not set at all.

    Here’s an example of how you can check if the email_verified_at field is null in your Blade view:

    @if (is_null($user->email_verified_at))
        <span class="text-danger">Not Verified</span>
    @else
        <span class="text-success">Verified At {{$user->email_verified_at}} </span>
    @endif
    

    This code uses the is_null() function to check if the email_verified_at field is null. Hope this will solve your issue.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search