laravelif-statementfor-else

how to make forelse and if branch in laravel


I apologize for the messy layout of my code syntax and maybe I don't really understand how to use @forelse and @if in Laravel, this code result is

syntax error, unexpected 'endif' (T_ENDIF)

                         <tbody>
                          @forelse ($kaders as $kader)
                            <tr>
                                <td>{{ $kader->username }}</td>
                                @if($kader->status_user == '1')
                                    <td>Admin</td>
                                @elseif($kader->status_user == '0')
                                    <td>Ibu / Guest</td>
                                @endif
                                @forelse ($surveillances as $surveillance)
                                    @if($kader->username == $surveillance->nik_ortu)
                                        @if($surveillance->status == '1')
                                        <td>Aktif</td>
                                        @elseif($surveillance->status == '0')
                                        <td>NonAKtif</td>
                                        @endif
                                    @endif
                                @endforelse
                                <td class="text-center">
                                    <form onsubmit="return confirm('Apakah Anda Yakin ?');" action="{{ route('kader.destroy', $kader->id) }}" method="POST">
                                        <a href="{{ route('kader.edit', $kader->id) }}" class="btn btn-sm btn-primary"><i class="far fa-edit"></i> EDIT</a>
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-sm btn-danger"><i class="far fa-trash-alt"></i> HAPUS</button>
                                    </form>
                                </td>
                            </tr>
                          @empty
                              <div class="alert alert-danger">
                                  Data Kader belum Tersedia.
                              </div>
                          @endforelse
                        </tbody>

Solution

  • You forgot to add @empty in @forelse loop

                                @forelse ($surveillances as $surveillance)
                                    @if($kader->username == $surveillance- 
                                            >nik_ortu)
                                        @if($surveillance->status == '1')
                                        <td>Aktif</td>
                                        @elseif($surveillance->status == '0')
                                        <td>NonAKtif</td>
                                        @endif
                                    @endif
                                 @empty
                                  //HEREEEE
                                @endforelse