skip to Main Content

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 AppHttpControllers;

use IlluminateHttpRequest;
use AppHttpRequestsStoreProgramRequest;
use AppHttpRequestsUpdateProgramRequest;
use IlluminateSupportFacadesGate;
use IlluminateHttpResponse;
use AppModelsProgram;

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

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

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

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

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

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

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

    /**
     * Update the specified resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  int  $id
     * @return IlluminateHttpResponse
     */
    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 IlluminateHttpResponse
     */
    public function destroy(Program $program)
    {
        $program->delete();

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

StoreProgramRequest.php

<?php

namespace AppHttpRequests;

use IlluminateFoundationHttpFormRequest;
use IlluminateSupportFacadesGate;

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'
        ];
    }
}

4

Answers


  1. All you need is to change @foreach to @forelse

    @forelse ($users as $user)
        <li>{{ $user->name }}</li>
    @empty
        <p>No users</p>
    @endforelse
    

    after editing will be like this

    <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>
    

    for more info check the docs here

    Login or Signup to reply.
  2. 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>
    
    Login or Signup to reply.
  3. I suggest this logic

    @forelse ($users as $user)
      <li>{{ $user->name }}</li>
    @empty
      <p>No users</p>
    @endforelse enter code here
    

    You can check the doc for more details https://laravel.com/docs/9.x/blade#loops

    Login or Signup to reply.
  4. @foreach ($test as $data)
    // do whatever you want 
    @if($data=="test")
       $val=1;
    @else
      $val=0;
    @endif
    
    @endforeach
    
    // If you are opening a directive its must be closed.
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search