Home > OS >  Yield blade file from another file not working
Yield blade file from another file not working

Time:04-19

I am trying to display comment index content

@extends('posts.show')

@section('title', 'Comments')

@section('comments')
<a href="{{route('comments.create', ['id'=>$post->id])}}">Create Comment</a>
    @foreach ($comments as $comment)
        @if ($comment->post_id == $post->id)
        <div >
            <div >
                <p>{{$comment->text}}</p>
            </div>
            <a href="{{route('comments.edit', $comment->id)}}" >Edit</a>
        </div>
        @endif
    @endforeach
<a href="{{route('posts.show', ['id'=>$post->id])}}">Back</a>
@endsection

From posts show file:

@extends('layouts.myapp')
@section('content')

<head>
  <link href="{{ asset('css/posts.css') }}" rel="stylesheet">
</head>
<div>
   <h2>Comments</h2>
   <div>
     @yield('comments')
   </div>
   <a href="{{route('comments.index', ['id' => $post->id])}}">Comments</a>
</div>
<a href="{{route('welcome')}}">Back</a>
@endsection

I want to be able to see the comments without having to go on comments.index link, which I have to delete.

CodePudding user response:

It's only a blind guess: maybe you should look at your blade directory structure, where the file with the comments-section is located, maybe it should be

@yield('posts.comments')

And by the way the head tag in the content section looks a bit displaced imho.

CodePudding user response:

The problem is that you are not yielding data here. You just include the value here. So use @include in stead of @yield. The following code is below:

In comment file :

 <a href="{{route('comments.create', ['id'=>$post->id])}}">Create Comment</a>
    @foreach ($comments as $comment)
        @if ($comment->post_id == $post->id)
        <div >
            <div >
                <p>{{$comment->text}}</p>
            </div>
            <a href="{{route('comments.edit', $comment->id)}}" >Edit</a>
        </div>
        @endif
    @endforeach
<a href="{{route('posts.show', ['id'=>$post->id])}}">Back</a>

Now in the From posts show file:

    @extends('layouts.myapp')
@section('content')

<head>
  <link href="{{ asset('css/posts.css') }}" rel="stylesheet">
</head>
<div>
   <h2>Comments</h2>
   <div>
     @include('comments')
   </div>
   <a href="{{route('comments.index', ['id' => $post->id])}}">Comments</a>
</div>
<a href="{{route('welcome')}}">Back</a>
@endsection
  • Related