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 >
{{ __('Program') }}
</h2>
</x-slot>
<div >
<div >
<a href="{{ route('program.create' )}}" >Tambah Program</a>
<br/>
@foreach ($program as $programs)
<a href="{{ route('program.show', $program->id) }}" >
<div >
<svg fill="none" viewBox="0 0 24 24"><!-- ... --></svg>
<h3 >{{ $program->year }}</h3>
</div>
</a>
<br/>
@empty
<tr >
<td colspan="2" >
{{ __('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'
];
}
}
CodePudding user response:
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 >
{{ __('Program') }}
</h2>
</x-slot>
<div >
<div >
<a href="{{ route('program.create' )}}" >Tambah Program</a>
<br/>
@forelse ($program as $programs)
<a href="{{ route('program.show', $program->id) }}" >
<div >
<svg fill="none" viewBox="0 0 24 24"><!-- ... --></svg>
<h3 >{{ $program->year }}</h3>
</div>
</a>
<br/>
@empty
<tr >
<td colspan="2" >
{{ __('No programs') }}
</td>
</tr>
@endforelse
</div>
</div>
</x-app-layout>
for more info check the docs here
CodePudding user response:
The @empty
directive needs to be closed as like other blade directives.
<x-app-layout>
<x-slot name="header">
<h2 >
{{ __('Program') }}
</h2>
</x-slot>
<div >
<div >
<a href="{{ route('program.create' )}}" >Tambah Program</a>
<br/>
@foreach ($program as $programs)
<a href="{{ route('program.show', $program->id) }}" >
<div >
<svg fill="none" viewBox="0 0 24 24"><!-- ... --></svg>
<h3 >{{ $program->year }}</h3>
</div>
</a>
<br/>
@endforeach
@empty($program)
<tr >
<td colspan="2" >
{{ __('No programs') }}
</td>
</tr>
@endempty
</div>
</div>
</x-app-layout>
Alternatively, use forelse
:
<x-app-layout>
<x-slot name="header">
<h2 >
{{ __('Program') }}
</h2>
</x-slot>
<div >
<div >
<a href="{{ route('program.create' )}}" >Tambah Program</a>
<br/>
@forelse ($program as $programs)
<a href="{{ route('program.show', $program->id) }}" >
<div >
<svg fill="none" viewBox="0 0 24 24"><!-- ... --></svg>
<h3 >{{ $program->year }}</h3>
</div>
</a>
<br/>
@empty
<tr >
<td colspan="2" >
{{ __('No programs') }}
</td>
</tr>
@endforelse
</div>
</div>
</x-app-layout>