Sumit kumar
Sumit kumar

Reputation: 402

How to call Variables in Blade @if Laravel

I'm working on this project where i can successfully achieve results as of this code in test:

$Counts = sentcount::find(Auth::user()->id)->TotalCount;
    $Counts2 = sentcount::find(Auth::user()->id)->SentCount;
    $Remaining = $Counts - $Counts2;
    if ($Remaining === 0){
        echo "You have reached your limits";
    } else {
        echo "You have" .$Remaining. " Remaining";
    }

But i want to use $Remaining my blade like: I'm new to blade and i know i can save remaining into my DB and then get data back like App\sentcount::find(\Illuminate\Support\Facades\Auth::user()->id)->Remaining === 0

I'm having trouble calling the variable in my @if & @else

my Blade Code:

@if ( App\sentcount::find(\Illuminate\Support\Facades\Auth::user()->id)->TotalCount > 0 )
                        <div class="text" style="max-width: 80%; padding-left: 3%; padding-top: 2%">
                        Usage:<br>
                        <h3 class="Sent" style="color: #1d68a7">Sent: {{  \App\sentcount::find(Auth::user()->id)->SentCount }}</h3>
                        <h3 class="Total" style="color: #2a9055">Remaining: {{  $Remaining }} </h3>
                        <div class="progress">
                            <div class="progress-bar" role="progressbar" style="width: {{  $Percent }}%;" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100">{{  $Percent }}%</div>
                        </div><br>
                        </div>
                        <hr>
                    @elseif( $Remaining === 0 )
                        You have reached your limits
                    @else
                        <div class="alert alert-dark" role="alert">
                            <h4 class="alert-heading">Oops!! You don't have any active subscription.</h4>
                            You can purchase a plan here <a href="Plans" class="alert-link">"Plans"</a>. <br>Open a support ticket for any help regarding purchases!.
                            <hr>
                        </div>
                    @endif

& Controller:

$Counts = sentcount::find(Auth::user()->id)->TotalCount;
        $Counts2 = sentcount::find(Auth::user()->id)->SentCount;
        $Percent = $Counts2/$Counts*100;
        $Remaining = $Counts - $Counts2;

Upvotes: 0

Views: 157

Answers (1)

mamaye
mamaye

Reputation: 1054

Assuming this is your controller

public function show(User $user){

           $remaining = 'your value';

            return view('folder.bladefile', compact('remaining'));//so we are passing the variable to our blade file

    }

In your blade file, you can do something like this

@if($remaining === 4)

//do this
//sometimes you might have to do count($remaining) if $remaining returns an array

@elseif($remaining < 1)

//you have reached your limit

@else

//do something else if none of the conditions are met

@endif

Upvotes: 2

Related Questions