got this error after inserting a @if inside a forelse and got a syntax error.
my home.blade.php:
@extends('master')
@section('header-intro')
@endsection
@section('main')
<div class="container">
<!--Section: Content-->
<section class="text-center">
<div class="posts">
@forelse ($posts as $post)
@if (auth()->user()->id === $post->user->id)
<div class="post">
<div class="card">
<div class="bg-image hover-overlay ripple" data-mdb-ripple-color="light">
<img src="{{ asset('storage/') . '/' . $post->thumb }}" class="img-post" />
<a href="#!">
<div class="mask" style="background-color: rgba(251, 251, 251, 0.15);"></div>
</a>
</div>
<div class="card-body">
<h5 class="card-title">{{ $post->title }}</h5>
<p class="card-text">
{{ Str::limit($post->body, 58, '...') }}
</p>
<p>Autor: {{ $post->user->name }} - has {{ $post->comments->count() }} comments</p>
<a href="{{ route('post', $post->slug) }}" class="btn btn-primary">read more</a>
</div>
</div>
</div>
@else
<h4>no posts for you</h4>
@endforelse
</div>
@if (request()->input('search'))
{{ $posts->appends(['search' => request()->input('search')])->links() }}
@endif
</section>
</div>
@endsection
Forelse is to show the posts, and I need the if to show the post only to the "owners" inside this page.
You need to end your if statement, and then end your forelse. When there are no elements in the $posts
array you call @empty
@endif
@empty
<h4>no posts for you</h4>
@endforelse
It's confusing because you are using an if statement inside a forelse statement.
Your else is for when there are no elements to iterate. But you aren't ending the if statement.
Technically, for completeness here I would recommend repeating your @else inside the loop too as you have a scenario where you might have elements in the $posts
array, but aren't for this auth'd user.
Perhaps something like:
@forelse ($posts as $post)
@if (auth()->user()->id === $post->user->id)
... your existing elements ...
@else
<h4>no posts for you</h4>
@endif
@empty
<h4>no posts</h4>
@endforelse