Yield blade file from another file not working - php

I am trying to display comment index content
#section('title', 'Comments')
Create Comment
#foreach ($comments as $comment)
#if ($comment->post_id == $post->id)
<div class="border margin mb-1">
<div class="border margin">
<p>{{$comment->text}}</p>
</div>
Edit
</div>
#endif
#endforeach
Back
#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>
#include('comments.index')
</div>
Comments
</div>
Back
#endsection
I want to be able to see the comments without having to go on comments.index link, which I have to delete.

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.

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 :
Create Comment
#foreach ($comments as $comment)
#if ($comment->post_id == $post->id)
<div class="border margin mb-1">
<div class="border margin">
<p>{{$comment->text}}</p>
</div>
Edit
</div>
#endif
#endforeach
Back
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>
Comments
</div>
Back
#endsection

Related

I'm working on a project in Laravel and I'm facing an Undefined variable $post error in post-card.blade.php

post.blade.php
The main post file
<main class="max-w-6xl mx-auto mt-6 lg:mt-20 space-y-6">
<x-post-featured-card :post="$posts[0]" />
<div class="lg:grid lg:grid-cols-2">
#foreach ($posts->skip(1) as $post)
<x-post-card :post="$post" />
#endforeach
</div>
</main>
post-card.blade.php
This file is throwing the undefined variable $post error
<div class="mt-4">
<h1 class="text-3xl">
{{ $post->title }}
</h1>
</div>
#props(['post'])
#foreach($posts as $post)
<div class="mt-4">
<h1 class="text-3xl">
{{ $post->title }}
</h1>
</div>
#endforeach
You can try a few steps
change the name of the file of post-card.blade.php
remove the other commented component from the file post-card.blade.php if have any
use props in the file that give error, like #props(['post'])

Include blade file from another file not working

I am trying to include this comments.index file:
#extends('posts.show')
#section('title', 'Comments')
#section('comments')
Create Comment
#foreach ($comments as $comment)
#if ($comment->post_id == $post->id)
<div class="border margin mb-1">
<div class="border margin">
<p>{{$comment->text}}</p>
</div>
Edit
</div>
#endif
#endforeach
Back
#endsection
From this posts.show file:
#extends('layouts.myapp')
#section('content')
<head>
<link href="{{ asset('css/posts.css') }}" rel="stylesheet">
</head>
<div class="post_container">
<div id="post_title">
{{$post->title}}
</div>
<div id="post_text">
{{$post->text ?? 'Empty'}}
</div>
<div>
<img src="{{ asset('storage/images/'. $post->image) }}">
<p>
{{$post->image}}
</p>
</div>
</div>
<div>
<h2>Comments</h2>
<div>
#include('comments')
</div>
Comments
</div>
Back
#endsection
Now the issue is that when I include 'comments' it throws 'view comments not found' error and asks if I am sure there is a blade file with such a name. This made me think that I need to include 'comments.index', but when I do this I get 'undefined variable $comments' error.
Doing an include of a partial is dot notation. So in this example...
#include( 'includes.modals.dude' )
This is really located in views -> includes -> modals -> dude.blade.php
The file name must be YourName.blade.php.
I think you are providing a relative path, include directive needs absolute path. So, in your case following code should work.
#include('comments.index')

why does repete #section content in Laravel

working with Laravel and I have following welcome page
</head>
<body>
</div><!--container-->
#include('partials._footer')
#endsection
#else
#section('fixx')
<p>Rivers</p>
#endsection
#endguest
</body>
</html>
and app.blade.php
#guest
<main class="py-4">
#yield('content')
</main>
#else
<main class="py-4">
#include('partials._nav')
#yield('fixx')
#include('partials._footer')
</main>
#endguest
_nav.blade.php
<link rel="stylesheet" href="{{ URL::asset('css/nav.css') }}" />
<div class="d-flex" id="wrapper">
<!-- Page Content -->
#yield('one')
#if('Auth')
#yield('two')
#endif
</div>
but when I see welcome page with logged user I could see #yield('one') contents in the #yield('two')
how could I fix this problem?
If you only want to show #yield('two') for logged in users, try this:
#auth
#yield('two')
#endauth
BTW, there are a few blade directives missing in your markup. Make sure you have a matching start and end tags for each directive you are using.
https://laravel.com/docs/blade#blade-directives
Edit:
If you want to hide #yield('one') for logged-in users:
// usign #guest
#guest
#yield('one')
#else
#yield('two')
#endauth

How to check foreach loop for a property. Laravel

I have been trying to figure out how to filter my foreach loop and only display an image if my post has one. So far I have been trying different functions and #ifs but to no avail.
Here is my controller code:
<div class="container">
<div class="row">
<div class="col-sm-6">
#foreach($posts as $post)
<div>
<h1>{{$post->title}}</h1>
<p>{{$post->body}}</p>
<img src="{{url('img', $post->image)}}">
</div>
<hr>
#endforeach
</div>
</div>
</div>
You can use a simple #if:
#if ($post->image)
<img src="{{ url('img/' . $post->image) }}">
#endif
Or #isset:
#isset ($post->image)
<img src="{{ url('img/' . $post->image) }}">
#endisset
https://laravel.com/docs/5.5/blade#control-structures
You can use a simple #if statement to see if the image property of the current $post is set. This could look a little something like this:
#foreach($posts as $post)
<div>
<h1>{{$post->title}}</h1>
<p>{{$post->body}}</p>
#if(!empty($post->image))
<img src="{{url('img', $post->image)}}">
#endif
</div>
<hr>
#endforeach
By including this, the <img> element will only be displayed if the property is not empty.
As also mentioned in another answer, you can replace the #if(!empty(...)) by #isset(...) to achieve the same result.

Blade template not showing section

I have these three blade templates
In my controller I'm calling dashboard view like this:
View::make('layouts.dashboard');
master.blade.php
<?= stylesheet_link_tag() ?>
<?= javascript_include_tag() ?>
<html>
<body class="fixed">
<div class="wrapper">
<div class="leftside">
#yield('leftside', 'left-side content')
</div>
<div class="rightside">
#yield('rightside', 'right-side content')
</div>
</div>
</body>
dashboard.blade.php
#extends('layouts.master')
#section('leftside')
#yield('sidebar', 'sidebar content')
#stop
sidebar.blade.php
#extends('layouts.dashboard')
#section('sidebar')
aaa
#stop
The dasbhoard blade shows properly in master blade, but sidebar blade doesn't want to show. Does anyone know what i'm doing wrong?
Thank your very much for any help :)
I don't think you can use yield() on a blade template that is not called.
Basically what you can do is make your master.blade.php into this
<html>
<body class="fixed">
<div class="wrapper">
<div class="leftside">
#include('sidebar') {{-- This is sidebar.blade.php --}}
</div>
<div class="rightside">
#include('dashboard') {{-- This is dashboard.blade.php --}}
</div>
</div>
</body>
</html>
Then, assuming that your are calling the dashboard.blade.php template, with both blade templates included. You can use the sections on either master.blade, or sidebar.blade inside dashboard.blade
#extends('layouts.master')
{{-- You can define now the sections available on master.blade and dashbard.blade --}}
OK I found out what was wrong :)
I called View::make('layouts.dashboard') instead of View::make('layouts.sidebar');

Categories