Error About Laravel Blade Foreach in Loop if else

There are problem my code about foreach and if condition. When I pulled else code block out of foreach loop there are no problem it is work. But I put in else code in loop. Else dont work and dont show anything.

My purpose, programme check to used user_id in total_bonuses datatable if ok I want to display only update form if no records user_id in total_bonuses I want to show store forum and user add to info. It seems to complicated.

My controller code is below

public function show($lang=null, $id)
    {
        $bonuses = DB::table('reservations')
         ->select(DB::raw('SUM(bonus) as total_bonus, user_id'))
         ->where('user_id', '=', $id)
         ->groupBy('user_id')
         ->get();

         $dolars = DB::table('reservations')
         ->select(DB::raw('SUM(dolar) as dolar, user_id'))
         ->where('user_id', '=', $id)
         ->groupBy('user_id')
         ->get();

         $confirmeds = DB::table('reservations')
         ->select(DB::raw('SUM(confirmation) as confirmed, user_id'))
         ->where('confirmation', '=', 1)
         ->where('user_id', '=', $id)
         ->groupBy('user_id')
         ->get();

        $user = DB::table('users')->find($id);
        $totals = DB::table('total_bonuses')->where('user_id', '=', $id)->get();
        return view('wallet.show', compact('totals', 'user', 'bonuses', 'dolars', 'confirmeds'));

    }

View Code is below

@foreach ($totals as $total)
@if ($total->new_dollar !== NULL) 
<div class="col-12"><a href="javascript:void(0)" class="link"><i class="mdi mdi-check-circle text-success"></i> <font class="font-medium"> {!! $total->new_dollar !!}
 Remaining Dollar</font></a></div>
@endif

                                    </div>
                                </center>
                            </div>
                            <div>
                    </div>

                        </div>
                    </div>
                    <!-- Column -->

                    <!-- Column -->

                    <div class="col-lg-8 col-xlg-9 col-md-7">
                        <div class="card">

@if (count($total->user_id) === 1)
<br>
<br>
    <label class="col-md-12"><h4>  Paid Dollar</h4></label>
<form class="form-horizontal form-material" method="POST" action="{{ route ('total.update', ['lang' => App::getLocale(), 'id' => $total->id]) }}">

         {{ csrf_field() }}
        <input type="hidden" name="_method" value="PATCH">
        <input type="hidden" name='id' value='{!! $total->id !!}'>
        <input type="hidden" name='user_id' value='{!! $user->id !!}'>
    <div class="form-group">
        <label class="col-md-12">Total Dollar</label>
        <div class="col-md-12">
            <input name="old_dollar" type="text" value="{!! $total->new_dollar !!}" class="form-control form-control-line" readonly>
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-12">Paid Dollar</label>
        <div class="col-md-12">
            <input name="paid_dollar" type="text" class="form-control form-control-line">
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-12">Note (Optionally! Not Required)</label>
        <div class="col-md-12">
            <input name="bonus_note" type="text" class="form-control form-control-line">
        </div>
    </div>
            <input name="moderator_id" type="hidden" value="{{ Auth::user()->id }}">
    <div class="form-group">
        <div class="col-sm-12">
            <button class="btn btn-success" type="submit">{{ __('profile.update')}}</button>
        </div>
    </div>
</form>

@else

<form class="form-horizontal form-material" method="POST" action="{{ route ('total.store', ['lang' => App::getLocale()]) }}">
{{ csrf_field() }}
<input type="hidden" name='user_id' value='{!! $user->id !!}'>
    <div class="form-group">
        <label class="col-md-12">Total Dollar</label>
        <div class="col-md-12">
            <input name="old_dollar" type="text" value="@foreach ($dolars as $dolar)
    {{ $dolar->dolar }}
@endforeach" class="form-control form-control-line" readonly>
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-12">Paid Dollar</label>
        <div class="col-md-12">
            <input name="paid_dollar" type="text" class="form-control form-control-line">
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-12">Note (Optionally! Not Required)</label>
        <div class="col-md-12">
            <input name="bonus_note" type="text" class="form-control form-control-line">
        </div>
    </div>
            <input name="moderator_id" type="hidden" value="{{ Auth::user()->id }}">
    <div class="form-group">
        <div class="col-sm-12">
            <button class="btn btn-success" type="submit">{{ __('profile.update')}}</button>
        </div>
    </div>
</form>
@endif

@endforeach

Best wishes.

1 answer

  • answered 2018-09-22 11:40 Varun.Kumar

    The problem in your provided view is with the if condition. Here you are trying to count the user id which is not possible:-

    @if (count($total->user_id) === 1)    
    

    Change the above code to

    @if (count($total) === 1)