Include blade file from another file not working - php

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')

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'])

Yield blade file from another file not working

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

Laravel 5.6 not working translate in content

I want to use translate in another file than main file(file where i have some content and this file is extended by main file), but it is not working. Do you know anyone why? The same problem is with theme_url('path').
in main file i have:
#php
App::setLocale('en');
Theme::Set('mobile');
#endphp
head...
<body>
#yield('content')
</body>
and in extended file:
#extends('main file')
#section('content')
<img src="{{ theme_url('img/logo.png') }}">
<span>{{ __('lang.title') }}</span>
#endsection
When i replaced #yield('content') by #include, all will be working(switching theme using from author igaster)
I think the problem is that
#extends('main file')
#section('content')
<img src="{{ theme_url('img/logo.png') }}">
<span>{{ __('lang.title') }}</span>
#endsection
this code executed first and then put in main file
so in this file locale is not 'en'
try this
#extends('main file')
App::setLocale('en');
Theme::Set('mobile');
#section('content')
<img src="{{ theme_url('img/logo.png') }}">
<span>{{ __('lang.title') }}</span>
#endsection

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