0

Here is my blade file foreach loop works perfectly but when i want to check the value exist or not then it always show not

@foreach ($value as $names)
<h3>{{$names}}</h3>
@endforeach

@if($names =="abc")
<h2>Found</h2>
@else
<h3>Not</h3>
@endif

and here is my controller

public function load_name()
    {
       
         $values = ['abc','xyz','yyy'];
         return view('name',['value' => $values]);
        
    }

and route

 Route::get("names",[UserController::class,'load_name']);
Alex
  • 2,126
  • 3
  • 25
  • 47
  • 1
    Because the `@if` block should be placed inside the `@foreach` loop. Currently it is outside – Clément Baconnier Apr 18 '22 at 18:52
  • Note that you can't tell if `abc` was not found until the end of the loop. Set a variable when the name matches, then check the variable at the end. Otherwise you'll print `Not` for every name that isn't `abc`. See https://stackoverflow.com/questions/42913798/searching-array-reports-not-found-even-though-its-found/42913882#42913882 – Barmar Apr 18 '22 at 18:59
  • It checks for every element. but is there any way to print the end of the loop for a particular single value is it exist in the array or not? – Imtiyaz Ratul Apr 19 '22 at 09:34

1 Answers1

0
@foreach ($value as $names)
  <h3>{{$names}}</h3>
  @if($names =="abc")
  <h2>Found</h2>
  @else
  <h3>Not</h3>
  @endif    
@endforeach
Gev99
  • 173
  • 5
  • It checks for every element. but is there any way to print the end of the loop for a particular single value is it exist in the array or not? – Imtiyaz Ratul Apr 18 '22 at 19:12