I have seen as "there are no posts" if they are available as comment even the URL is working property.
I have discover on laravel.com there is not lack of information or example inside the #if and #else . http://laravel.com/docs/4.2/templates#other-blade-control-structures.
here is my code is
#if ($post->comments)
<div class="text-center jumbotron">
<h1 style="color:red;">There are no posts</h1>
</div>
#endif
#foreach ($post->comments as $comment)
<div class="row">
<div class="col-xs-12">
{{{ $comment->user->name }}}:
<blockquote>{{{ $comment->text}}}</blockquote>
</div>
</div>
#endforeach
however I have tried change as #if ($post->comments as $comment) is said "syntax error, unexpected 'as' (T_AS)" do you have any idea to solve it?
or you could use the blade's #forelse loop this way
#forelse($post->comments as $comment)
<div class="row">
<div class="col-xs-12">
{{{ $comment->user->name }}}:
<blockquote>{{{ $comment->text}}}</blockquote>
</div>
</div>
#empty
<div class="text-center jumbotron">
<h1 style="color:red;">There are no posts</h1>
</div>
#endforelse
Just a guess, but I think this is what you wanted to do
#if ($post->comments)
#foreach ($post->comments as $comment)
<div class="row">
<div class="col-xs-12">
{{{ $comment->user->name }}}:
<blockquote>{{{ $comment->text}}}</blockquote>
</div>
</div>
#endforeach
#else
<div class="text-center jumbotron">
<h1 style="color:red;">There are no posts</h1>
</div>
#endif
You are trying to put a foreach in the condition of an if statement
Put the foreach in the entire #if, and check for NOT comments :
#if (!$post->comments)
<div class="text-center jumbotron">
<h1 style="color:red;">There are no posts</h1>
</div>
#else
#foreach ($post->comments as $comment)
<div class="row">
<div class="col-xs-12">
{{{ $comment->user->name }}}:
<blockquote>{{{ $comment->text}}}</blockquote>
</div>
</div>
#endforeach
#endif
This should be the control flow you are looking for.
Related
I'm developing a website like instagram in laravel
everything was going fine until i implemented the foreach command
this is my code:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-3 p-5">
<img src="/images/simbolomax.jpg" class="rounded-circle">
</div>
<div class="col-9 pt-5">
<div class="d-flex justify-content-between align-items-baseline">
<h1 class="p-2">{{ $user->username }}</h1>
Aiciona nova receita
</div>
<div class="d-flex">
<div class="pe-5"><strong>153</strong> receitas</div>
<div class="pe-5"><strong>23k</strong> seguidores</div>
<div class="pe-5"><strong>212</strong> seguindo</div>
</div>
<div class="pt-4">{{ $user->profile->description }}</div>
</div>
</div>
<div class="row pt-5">
#foreach($user->posts as $post)
<div class="col-4">
<img src="/storage/{{$post->image}}" class="w-100" style="height:100%">
</div>
#endforeach
</div>
</div>
#endsection
and the error is: Invalid argument supplied for foreach() (View: C:\projetos\easymeal\resources\views\profiles\index.blade.php)
I can't figure out, send help!
If there is a user with no posts... There goes ERROR. So.... Check user for posts before #foreach.
#if(count($user->posts)>0).... #foreach
Try this
#if ($user->posts->isNotEmpty())
#foreach ($user->posts as $item)
<div class="col-4">
<img src="/storage/{{$post->image}}" class="w-100" style="height:100%">
</div>
#endforeach
#endif
<h2 class="section-subheading text-muted text-center">الكورس الاول</h2>
<div class="row text-center">
#foreach ($data as $item)
<div class="col-md-4 px-3">
<div class="card shadow p-4">
<h5 class="text-center">{{$item->name}}</h5>
<hr>
show lectures
</div>
</div>
#endforeach
<h2 class="section-subheading text-muted text-center">الكورس الثاني</h2>
#foreach ($data2 as $item)
<div class="col-md-4 px-3">
<div class="card shadow p-4">
<h5 class="text-center">{{$item->name}}</h5>
<hr>
show lectures
</div>
</div>
#endforeach
</div>
If I'm understanding your question correctly.
You can nest #foreach statements like so
#foreach ($list1 as $item1)
...
#foreach ($list2 as $item2)
...
#endforeach
...
#endforeach
You would obviously want to use better names for the variables than in my sample. You do not need to have additional statements between the loops. The "..." is there to show you can.
#foreach ($data as $item)
{{ $item->name }}
#foreach ($data2 as $item2)
{{ $item2->name }}
#endforeach
#endforeach
I'm using the materialisecss CSS framework within my Laravel Views, and I'm trying to set my layout such that:
if there is only one post available, it is displayed 6 columns wide,
offset by 3
if there are only 2 posts, they are both displayed 6 columns wide side by side
if there are more than 2 posts they are displayed 4 columns wide.
I have the code below as part of my view, but it doesn't like it, and returns the following:
Parse error: syntax error, unexpected 'elseif' (T_ELSEIF) (View: /home/testuser/sites/lsapp/resources/views/posts/index.blade.php)
#if(count($posts) > 0)
<div class="row">
#if (count($posts) === 1)
#foreach($posts as $post)
<div class="col s12 m6 offset-m3">
#elseif (count($posts) === 2)
#foreach($posts as $post)
<div class="col s12 m6">
#else
#foreach($posts as $post)
<div class="col s12 m4">
#endif
<a href="posts/{{$post->id}}">
<div class="card hoverable">
<div class="card-image">
<img src="http://via.placeholder.com/460x230">
<span class="card-title">{{$post->title}}</span>
<a class="btn-floating halfway-fab waves-effect waves-light red hover"><i class="material-icons">remove_red_eye</i></a>
</div>
<div class="card-content">
<p>
Created by {{$post->user->name}}<br>
<small>on {{$post->created_at->toDateString()}} at {{$post->created_at->toTimeString()}}</small>
</p>
</div>
</div>
</a>
</div>
#endforeach
</div>
#else
<div class="row">
<div class="col s12">
<div class="card">
<p>No posts found</p>
</div>
</div>
</div>
#endif
I think it has something to do with the foreach, but I'm not certain.
Note: There is probably a far better way of doing this, but I'm currently learning Laravel after teaching myself PHP, so it's not the end of the world.
You're getting the problem because you're not closing off the directives properly. Try something like this.
#if(count($posts) > 0)
<div class="row">
#foreach($posts as $post)
<div class="#if (count($posts) === 1) col s12 m6 offset-m3 #elseif (count($posts) === 2) col s12 m6 #else col s12 m4 #endif">
<a href="posts/{{$post->id}}">
<div class="card hoverable">
<div class="card-image">
<img src="http://via.placeholder.com/460x230">
<span class="card-title">{{$post->title}}</span>
<a class="btn-floating halfway-fab waves-effect waves-light red hover"><i
class="material-icons">remove_red_eye</i></a>
</div>
<div class="card-content">
<p>
Created by {{$post->user->name}}<br>
<small>on {{$post->created_at->toDateString()}}
at {{$post->created_at->toTimeString()}}</small>
</p>
</div>
</div>
</a>
</div>
#endforeach
</div>
#else
<div class="row">
<div class="col s12">
<div class="card">
<p>No posts found</p>
</div>
</div>
</div>
#endif
You need to #endforeach
#foreach($posts as $post)
....
#endforeach
The error is saying that there is an unexpected #elseif because the parser expects you to end the foreach before.
you can try this:
#if (count($posts) === 1)
<?php $class = 'col s12 m6 offset-m3'; ?>
#elseif (count($posts) === 2)
<?php $class = 'col s12 m6'; ?>
#else
<?php $class = 'col s12 m4'; ?>
#endif
#foreach($posts as $post)
<div class="{{ $class }}">
//rest of code...
#endforeach
Yes every directive must be closed properly. Meaning you should have the closing tag #endforeach after every #foreach statement.
If it's difficult. You can try
#php #endphp
I have created a Post and Comment application. I am trying to link the comments to the id of the post. I am trying to use PHP in a Blade template to do this. I get the error that the variable post_id does not exist. I want to use the #if and #foreach of Blade. The problem seems to be in the #if statement.
Here is my HTML:
<div class="col-md-9">
<div class="row">
#foreach($posts as $post)
<div class="col-md-12 post" id="post{{ $post->id }}">
<div class="row">
<div class="col-md-12">
<h4>
{{ $post->title }}
</h4>
</div>
</div>
<div class="row">
<div class="col-md-12 post-header-line">
<span class="glyphicon glyphicon-user"></span> by {{ $post->user->firstName }} {{ $post->user->lastName }} | <span class="glyphicon glyphicon-calendar">
</span> {{ $post->created_at }} | <span class="glyphicon glyphicon-comment"></span><a href="#">
3 Comments</a>
</div>
</div>
<div class="row post-content">
<div class="col-md-2">
<a href="#">
<img src="/images/random/postimg.jpg" alt="" class="img-responsive postImg">
</a>
</div>
<div class="col-md-10">
<p>
{{ $post->post }}
</p>
</div>
</div>
<div class="row add-comment">
<div class="form-group">
<input type="text" name="addComment" placeholder="Add your comment" class="form-control" v-model="comment">
</div>
<input id="addComment" type="submit" name="submitComment" value="Submit Comment" class="btn btn-default" v-on:click="submitComment" data-id="{{ $post->id }}">
</div>
#if($post->comment->post_id == $post->id)
#foreach($post->comment as $comment)
<div class="row">
<div class="col-md-12 mb-r">
<div class="card example-1 scrollbar-ripe-malinka">
<div class="card-body">
<h4 id="section1"><strong>By: {{ $comment->user->firstName }} {{ $comment->user->lastName }}</strong></h4>
<p>{{ $comment->comment }}</p>
</div>
</div>
</div>
</div>
#endforeach
#endif
</div>
#endforeach
</div>
</div>
Here is my PostController:
public function index(){
$posts = Post::with('comment', 'user')->orderBy('id', 'desc')->limit(20)->get();
return view('post.posts')->with('posts', $posts);
}
You don't need #if statement, remove it. $post->comment is a collection, that 's why your are not getting post_id. if you want to check, then check inside foreach.
#foreach($post->comment as $comment)
<div class="row">
<div class="col-md-12 mb-r">
<div class="card example-1 scrollbar-ripe-malinka">
<div class="card-body">
<h4 id="section1"><strong>By: {{ $comment->user->firstName }} {{ $comment->user->lastName }}</strong></h4>
<p>{{ $comment->comment }}</p>
</div>
</div>
</div>
</div>
#endforeach
You need to add post_id to the comments table and use the relationship:
public function comments()
{
return $this->hasMany(Comment::class);
}
Then you'll be able to load related comments:
Post::with('comments', ....
And iterate over posts and their comments:
#foreach ($posts as $post)
#foreach ($post->comments as $comment)
{{ $comment->content }}
#endforeach
#endforeach
I think if you create relationship between Post model and Comment model (post table and comments table), your #if is useless. I recommend you to check your corresponding models.
Your code should be like this,
Post.php
public function comments(){
return $this->morphMany('App\Comments','commentable');
}
Comment.php
public function commentable(){
return $this->morphTo();
}
in my app Comment model is shared by some other models also.
Eg :- User’s Questions also have comments.
Find more details in laravel:Eloquent page
I need to create a row for each 3 attachments. Something like
#foreach($email->attachment as $attach)
<div class="row">
<div class="col-lg-4"></div>
<div class="col-lg-4"></div>
<div class="col-lg-4"></div>
</div>
#endforeach
<div class="row">
<div class="col-lg-4">1st attachment</div>
<div class="col-lg-4">2nd attachment</div>
<div class="col-lg-4">3rd attachment</div>
</div>
<div class="row">
<div class="col-lg-4">4th attachment</div>
<div class="col-lg-4">5th attachment</div>
<div class="col-lg-4">6th attachment</div>
</div>
and so on! I had searched for the question but just found This link but It's actually different from my perspective.
Thanks
Use array_chunk - it is designed for this each purpose - so you can cut an array into sub-sizes and loop through each group
#foreach (array_chunk($email->attachment->toArray(), 3, true) as $array)
<div class="row">
#foreach($array as $attachment)
<div class="col-lg-4">{{ $attachment['id'] }}</div>
#endforeach
</div>
#endforeach
And using #Lukasgeiter's comment - you could also do it this way if it is a Laravel Collection
#foreach ($email->attachment->chunk(3) as $array)
<div class="row">
#foreach($array as $attachment)
<div class="col-lg-4">{{ $attachment->id }}</div>
#endforeach
</div>
#endforeach
Try this, it's faster than generate 2 arrays:
<div class="row">
#foreach( $email->attachment as $index => $attach)
<div class="col-lg-4">{{$attach}}</div>
#if( $index+1 === 0)
</div><div class="row">
#endif
#endforeach
</div>