Как называть переменные в Blade @if Laravel - PullRequest
0 голосов
/ 29 октября 2019

Я работаю над этим проектом, в котором я могу успешно достичь результатов, указанных в тестируемом коде:

$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";
    }

Но я хочу использовать $ Остается мой блейд, например:я знаю, что могу сохранить оставшиеся данные в моей БД и затем получить данные обратно, как App\sentcount::find(\Illuminate\Support\Facades\Auth::user()->id)->Remaining === 0

У меня проблемы с вызовом переменной в моем @if & @else

моем блейд-коде:

@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

& Контроллер:

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

1 Ответ

2 голосов
/ 29 октября 2019

Предполагая, что это ваш контроллер

public function show(User $user){

           $remaining = 'your value';

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

    }

В вашем блейд-файле вы можете сделать что-то вроде этого

@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
...