1

I can't do a foreach with my response

I already tried many things but nothing resolved it

/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
  $client = new \GuzzleHttp\Client();
  $effects = [];
  $res = $client->request('GET', 'http://85.171.71.189/get_modes');
  $effects = json_decode($res->getBody()->getContents(), true);
  return view('base.effects', [ 'effects' => $effects ]);
}
@if($effects)
  @foreach($effects as $effect)
    <tr>
      <td>{{ $effect['mode'] }}</td>
      <td>{{ $effect['name'] }}</td>
      <td><button class="btn btn-sm btn-primary">Activate</button></td>
    <tr>
  @endforeach
@else
  <tr>
    <td colspan="3">No effects found.</td>
  </tr>
@endif

I expect that I have a table with all modes inside, but the actual error is

Undefined index: name (View: H:\wamp64\www\scintillement\resources\themes\light\base\effects.blade.php)

Jehanzeb.Malik
  • 3,332
  • 4
  • 25
  • 41
  • 1
    dd($effects) in your controller and check if the name index exists. – zahid hasan emon Jul 23 '19 at 11:10
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Script47 Jul 23 '19 at 11:11

3 Answers3

2

Try this:

@forelse ($effects as $effect)
    <tr>
        <td>{{ isset($effect['mode']) ? $effect['mode'] : '--' }}</td>           
        <td>{{ isset($effect['name'] ? $effect['name'] : '--' }}</td>                
        <td><button class="btn btn-sm btn-primary">Activate</button></td>
    <tr>
@empty
    <tr>
        <td colspan="3">No effects found.</td>
    </tr>
@endforelse

Immeyti
  • 545
  • 3
  • 15
1

You should try this:

 @if($effects)
                  @foreach($effects as $effect)
                      <tr>
                        @if(isset($effect['mode']))
                        <td>{{ $effect['mode'] }}</td>
                        @else
                        <td>-</td>
                        @endif
                        @if(isset($effect['name']))
                        <td>{{ $effect['name'] }}</td>
                        @else
                        <td>-</td>
                        @endif
                        <td><button class="btn btn-sm btn-primary">Activate</button></td>
                      <tr>
                  @endforeach
                @else
                  <tr>
                    <td colspan="3">No effects found.</td>
                  </tr>
                @endif
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
1

There were some empty rows in get from the http://85.171.71.189/get_modes

You can handle it by adding condition of empty or not before displaying in the table

@if($effects)
@foreach($effects as $effect)
    @if(!empty($effect)) // new condition added
        <tr>
            <td>{{ $effect['mode'] }}</td>
            <td>{{ $effect['name'] }}</td>
            <td><button class="btn btn-sm btn-primary">Activate</button></td>
        <tr>
    @endif
@endforeach