簡體   English   中英

語法錯誤,意外標記“endforeach”,需要“elseif”或“else”或“endif”

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

所以我做了一個jetstream項目,這是我第一次使用jetstream。 我的問題是,由於某種原因,程序的索引頁說我沒有關閉 foreach 循環,而實際上我已經關閉了。 我很確定它一直在工作,直到我添加了一種在列中存儲數據的方法,然后它就沒有工作了。 存儲功能有問題還是@有問題?

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

@empty指令需要像其他刀片指令一樣關閉。

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

或者,使用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>

您只需要將@foreach更改為@forelse

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

編輯后會像這樣

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

有關更多信息,請在此處查看文檔

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

我建議這個邏輯

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

您可以查看文檔了解更多詳細信息https://laravel.com/docs/9.x/blade#loops

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM