zerodps
2 months ago

@foreach loop not working? laravel 5.8.24 (WAMP) W10

Posted 2 months ago by zerodps

Hello everybody, I'm working on the series "Let's build a forum with Laravel." In episode 3, Jeffrey displays the thread-related answers with a foreach loop. The problem I have is that the browser does not display HTML elements after my foreach loop. When I comment out the loop and its code, the HTML elements are displayed in the browser.

My threads model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Thread extends Model
{
    public function path()
    {
        return '/threads/' . $this->id;
    }
    
    public function replies()
    {
        return $this->hasMany(Reply::class);
    }
}

My ThreadsController

    public function show(Thread $thread)
    {
        return view('threads.show', compact('thread'));
    }

my show.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{$thread->title}}</div>
                <div class="card-body">
                    {{ $thread->body }}                     
                </div>
            </div>
        </div>
    </div>
    <div class="row justify-content-center">
            @foreach ($thread->replies as $reply)
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">{{ $reply->created_at }}</div>
                    <div class="card-body">
                             
                                     <h4>
                                        {{ $reply->created_at }}   
                                    </h4> 
                                    <div class="body"> {{ $reply->body }} </div>
                           
                    </div>
                </div>
            </div>
            @endforeach
        </div>
</div>   


@endsection


After several hours of trying different approaches, I now hope for your support.

Thanks in advance

Please sign in or create an account to participate in this conversation.