Learning

How to use if else conditions in laravel blade view or for html code?

Hello, thanks for checking out here!

Here is the quick and short answer with the examples:

Example 1
<img src="@if($category->image) {{ ($category->image) }} @else {{'https://via.placeholder.com/50'}} @endif" alt="{{ $category->name}}" width="50" height="50" />

This is what I was looking for my part of development laravel! same you can achieve for the html blocks: here is the example:

Example 2
@if ($message = Session::get('success'))
    <script>
      if(window.toastr)
          toastr["success"]("{{ session()->get('success') }}"); 
    </script> 
@endif
Example 3
@if ($message = Session::get('success'))
  <div class="alert alert-success">
     session()->get('success')
  </div>
@endif

To use elseif just use following syntax @elseif(condition) along your code or between.

Thanks & Happy learning Laravel!

admin

Recent Posts

The Ultimate Guide to Effective Meetings

In today’s fast-paced work environment, meetings are essential but can often feel unproductive. However, by…

1 week ago

From Mine to Market: How Gold Is Mined, Refined, and Sold in Shops

Gold is one of the most coveted precious metals in the world, adored for its…

1 week ago

The Historical Journey of Gold: From Ancient Civilizations to Modern Times

Gold, the shimmering metal synonymous with wealth, power, and beauty, has been deeply intertwined with…

1 week ago

How to Onboard an Intern in Small, Individual-Based Company

How to Onboard an Intern in a Small, Individual-Based Company Hiring an intern can be…

2 months ago