Jeremy
Jeremy

Reputation: 1952

How to don't display a block when the value is in a foreach inside the block

I'm trying not to show the div when my foreach doesn't contain any value.

<div class="box box-solid">
        <div class="box-body">
            <h4 style="background-color:#f7f7f7; font-weight: 600; font-size: 18px; text-align: center; padding: 7px 10px; margin-top: 0;">
                MANUELS
            </h4>
            <div>
                @foreach($filesFromUser as $file)
                    @if($file->category->name == 'Manuels')
                        <a href="{{ Url('/') . '/' . $file->file_url }}" class="btn btn-block btn-primary btn-flat" target="_blank">{{ $file->name }}</a>
                    @endif
                @endforeach
            </div>
        </div>
    </div>

The problem is that the foreach is declared inside the block "box box-solid", so I do not count (). What is the solution ? I want to hide "box box-solid".

Thank you very much !

Upvotes: 0

Views: 40

Answers (2)

Mohan Raj
Mohan Raj

Reputation: 116

Instead of doing this in view file, you can filter the collection in controller method before send the data to the view like,

$filesFromUser = $filesFromUser->filter(function ($file) {
    return $file->->category->name == 'Manuels';
});
return view('view-path', compact('filesFromUser'));

And then in your view,

@if($filesFromUser->count())
 <div class="box box-solid">
    <div class="box-body">
        <h4 style="background-color:#f7f7f7; font-weight: 600; font-size: 18px; text-align: center; padding: 7px 10px; margin-top: 0;">
            MANUELS
        </h4>
        <div>
            @foreach($filesFromUser as $file)
                @if($file->category->name == 'Manuels')
                    <a href="{{ Url('/') . '/' . $file->file_url }}" class="btn btn-block btn-primary btn-flat" target="_blank">{{ $file->name }}</a>
                @endif
            @endforeach
        </div>
    </div>
  </div>
@endif

Upvotes: 1

J. Doe
J. Doe

Reputation: 1732

I think this will help

<?php
$string = '';
foreach($filesFromUser as $file) {
     if($file->category->name == 'Manuels') {
          $string .= '<a href="{{ Url('/') . '/' . $file->file_url }}" class="btn btn-block btn-primary btn-flat" target="_blank">{{ $file->name }}</a>';
     }
}
?>

<div class="box box-solid">
    <div class="box-body">
        <h4 style="background-color:#f7f7f7; font-weight: 600; font-size: 18px; text-align: center; padding: 7px 10px; margin-top: 0;">
            MANUELS
        </h4>
            @if(!empty($string))
              <div>{{ $string }}</div>
            @endif
    </div>
</div>

Upvotes: 2

Related Questions