Search code examples
phplaravellaravel-jetstream

syntax error, unexpected token "endforeach", expecting "elseif" or "else" or "endif"


So I made a jetstream project, this is my first time using jetstream. My problem is that for some reason the index page of the program says that I didn't close the foreach loop, when actually I have. I am pretty sure it was working until I added a way to store a data in the column and it didn't work after that. Is there a problem with the storing feature or is there a problem with the @?

program/index.blade.php

<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Program') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
          <a href="{{ route('program.create' )}}" class="bg-green-500 hover:bg-green--700 text-white font-bold py-2 px-4 rounded">Tambah Program</a>
          <br/>

          @foreach ($program as $programs)
            <a href="{{ route('program.show', $program->id) }}" class="group block max-w-xs mx-auto rounded-lg p-6 bg-white ring-1 ring-slate-900/5 shadow-lg space-y-3 hover:bg-sky-500 hover:ring-sky-500">
              <div class="flex items-center space-x-3">
                <svg class="h-6 w-6 stroke-sky-500 group-hover:stroke-white" fill="none" viewBox="0 0 24 24"><!-- ... --></svg>
                <h3 class="text-slate-900 group-hover:text-white text-sm font-semibold">{{ $program->year }}</h3>
              </div>
            </a>
          <br/>
          @empty
            <tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700">
              <td colspan="2" class="px-6 py-4 font-medium text-gray-900 dark:text-white whitespace-nowrap">
                {{ __('No programs') }}
              </td>
            </tr>
          @endforeach          
        </div>
    </div>
</x-app-layout>

ProgramController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\StoreProgramRequest;
use App\Http\Requests\UpdateProgramRequest;
use Illuminate\Support\Facades\Gate;
use Illuminate\Http\Response;
use App\Models\Program;

class ProgramController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $program = Program::all();

        return view('program.index', compact('program'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('program.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreProgramRequest $request)
    {
        Program::create($request->validated());

        return redirect()->route('program.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Program $program)
    {
        return view('program.show', compact('program'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Program $program)
    {
        return view('program.edit', compact('program'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateProgramRequest $request, Program $program)
    {
        $program->update($request->validated());

        return redirect()->route('program.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Program $program)
    {
        $program->delete();

        return redirect()->route('program.index');
    }
}

StoreProgramRequest.php

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;

class StoreProgramRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        return [
            'tahun' => 'required'
        ];
    }
}

Solution

  • The @empty directive needs to be closed as like other blade directives.

    <x-app-layout>
        <x-slot name="header">
            <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                {{ __('Program') }}
            </h2>
        </x-slot>
    
        <div class="py-12">
            <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
              <a href="{{ route('program.create' )}}" class="bg-green-500 hover:bg-green--700 text-white font-bold py-2 px-4 rounded">Tambah Program</a>
              <br/>
    
              @foreach ($program as $programs)
                <a href="{{ route('program.show', $program->id) }}" class="group block max-w-xs mx-auto rounded-lg p-6 bg-white ring-1 ring-slate-900/5 shadow-lg space-y-3 hover:bg-sky-500 hover:ring-sky-500">
                  <div class="flex items-center space-x-3">
                    <svg class="h-6 w-6 stroke-sky-500 group-hover:stroke-white" fill="none" viewBox="0 0 24 24"><!-- ... --></svg>
                    <h3 class="text-slate-900 group-hover:text-white text-sm font-semibold">{{ $program->year }}</h3>
                  </div>
                </a>
              <br/>
              @endforeach
    
              @empty($program)
                <tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700">
                  <td colspan="2" class="px-6 py-4 font-medium text-gray-900 dark:text-white whitespace-nowrap">
                    {{ __('No programs') }}
                  </td>
                </tr>
              @endempty
              
            </div>
        </div>
    </x-app-layout>
    

    Alternatively, use forelse:

    <x-app-layout>
        <x-slot name="header">
            <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                {{ __('Program') }}
            </h2>
        </x-slot>
    
        <div class="py-12">
            <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
              <a href="{{ route('program.create' )}}" class="bg-green-500 hover:bg-green--700 text-white font-bold py-2 px-4 rounded">Tambah Program</a>
              <br/>
    
              @forelse ($program as $programs)
                <a href="{{ route('program.show', $program->id) }}" class="group block max-w-xs mx-auto rounded-lg p-6 bg-white ring-1 ring-slate-900/5 shadow-lg space-y-3 hover:bg-sky-500 hover:ring-sky-500">
                  <div class="flex items-center space-x-3">
                    <svg class="h-6 w-6 stroke-sky-500 group-hover:stroke-white" fill="none" viewBox="0 0 24 24"><!-- ... --></svg>
                    <h3 class="text-slate-900 group-hover:text-white text-sm font-semibold">{{ $program->year }}</h3>
                  </div>
                </a>
              <br/>
              @empty
                <tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700">
                  <td colspan="2" class="px-6 py-4 font-medium text-gray-900 dark:text-white whitespace-nowrap">
                    {{ __('No programs') }}
                  </td>
                </tr>
              @endforelse
              
            </div>
        </div>
    </x-app-layout>