How to check foreach loop for a property. Laravel - php

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.

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

Get all images from the database and display the images in a bootstrap 4 carousel Laravel 5.8

I've been trying to get all the images from the database and trying to display them in a bootstrap carousel but it is only returning one image.
<div class="carousel-item active">
#if($posts->active == 1)
<img src="{{ Storage::url("image/{$posts->image}") }}" alt="..." class="d-block img-fluid">
#endif
<div class="carousel-caption d-none d-md-block">
<h3 class="text-white">{{ $posts->title }}</h3>
<p>{{ $posts->h1 }}</p>
</div>
</div>
And here is the code in the controller index function
$posts = Post::latest()->first();
When I try to change the method to get() instead of first() an error appears. I'm not sure if I need to setup a new variable for the images, or include a foreach in the view which I'm not sure how to do. Really appreciate the help.
<div class="carousel-item active">
#foreach($posts as $post)
#if($post->active == 1)
<img src="{{ Storage::url("image/{$post->image}") }}" alt="..." class="d-block img-fluid">
#endif
<div class="carousel-caption d-none d-md-block">
<h3 class="text-white">{{ $post->title }}</h3>
<p>{{ $post->h1 }}</p>
</div>
#endforeach
</div>
You can access your posts like this if you are using get() method.
Since first() method directly returns you the model you can access its properties but for get() you will have to loop through it since it returns "Eloquent Collection' (not array, you can use toArray() if you need one).

laravel foreach and if else loop errors both in count and variables something

code of 1st error :
#extends('layouts.app')
#section('content')
<h1> posts</h1>
#if(count($posts)>1)
#foreach($posts as $post)
<div class="well">
<h3>{{$post->title}}</h3>
</div>
#endforeach
#else
<p> no posts found</p>
#endif
#endsection
and image also attached .
[enter image description here][1]
after i follow the instructions of the chrome this occurs and code will be .. even i have removed the # and try if() like this ..
code 2 ;
#extends('layouts.app')
#section('content')
<h1> posts</h1>
#if(count($posts ?? '')>1)
#foreach($posts ?? '' as $post)
<div class="well">
<h3>{{$post->title}}</h3>
</div>
#endforeach
#else
<p> no posts found</p>
#endif
#endsection
Error images:
https://i.stack.imgur.com/21weC.png
https://i.stack.imgur.com/RDdF2.png
A shorter approach for this would be:
#forelse($posts as $post)
<div class="well">
<h3>{{$post->title}}</h3>
</div>
#empty
<p> no posts found</p>
#endforelse
You can find more loops available in the documentation:
https://laravel.com/docs/7.x/blade#loops
Use forelse instead of foreach, that should do the job

How to create dynamic layout in Laravel with Blade?

So I'm building my first blog, and I'd like to dynamically show to posts on my landing page. I want the layout to become like this (might be a little weird):
post-image - post-preview
post-preview - post-image
post-image - post-preview
This is what I have right now to show to blog posts:
#foreach($posts as $post)
<div class="col-sm-4">
<a href="/post/{{ $post->slug }}">
<img src="{{ Voyager::image( $post->image ) }}" class="img-responsive" style="width:100%">
<span>{{ $post->title }}</span>
</a>
</div>
#endforeach
web.php (route):
$posts = App\Post::take(3)->orderBy('created_at')->get();
return view('welcome', compact('posts'));
Now as I'd like to make the layout dynamically. I think I can do this by checking if the ID even or odd. However I have no idea on how to do this even when I've searched 50% of the web already :) .
If there is a better way on doing this just let me know! Greatly appreciated guys!
Yes you can do it by checking if current post or loop iteration is odd or even. (https://laravel.com/docs/5.5/blade#the-loop-variable) For Odd loop iteration, you can write image code first and in even loop iteration you can write preview code first to achieve your desired layout. You can check the following code:
#foreach($posts as $post)
<div class="row">
#if( $loop->iteration % 2 == 0 )
<div class="col-md-4">
<a href="/post/{{ $post->slug }}">
<img src="{{ Voyager::image( $post->image ) }}" class="img-responsive" style="width:100%">
</a>
</div>
<div class="col-md-8">
<span>{{ $post->title }}</span>
</div>
#else
<div class="col-md-8">
<span>{{ $post->title }}</span>
</div>
<div class="col-md-4">
<a href="/post/{{ $post->slug }}">
<img src="{{ Voyager::image( $post->image ) }}" class="img-responsive" style="width:100%">
</a>
</div>
#endif
</div>
#endforeach
Ok I guess I got your question. so there is no direct solution but you can measure inside your for loop by adding extra variable such as:
<?php $inc = 0;?>
#foreach($posts as $post)
<div class="col-sm-4">
<a href="/post/{{ $post->slug }}">
#if($inc%2 == 0)
<img src="{{ Voyager::image( $post->image ) }}" class="img-responsive" style="width:100%">
<span>{{ $post->title }}</span>
#else
<span>{{ $post->title }}</span>
<img src="{{ Voyager::image( $post->image ) }}" class="img-responsive" style="width:100%">
#endif
<?php $inc++;?>
</a>
</div>
#endforeach
Or you can use for loop instead of foreach of loop and inside for loop you can check variable i.e. i is odd or even.

How to skip first item in a foreach loop in Laravel?

I am trying to fill my webpage with content based on content stored in a database. However, I would like to skip the first item; I want to start looping from the second item.
How can I achieve this?
#foreach($aboutcontent as $about)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endforeach
As of Laravel 5.4, whenever you use foreach or for within blade files you will now have access to a $loop variable. The $loop variable provides many useful properties and methods, one of them being useful here, for skipping the first iteration. See the example below, which is a much cleaner way of achieving the same result as the other older answers here:
#foreach ($rows as $row)
#if ($loop->first) #continue #endif
{{ $row->name }}<br/>
#endforeach
Try This :
#foreach($aboutcontent as $key => $about)
#if($key > 0){
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endif;
#endforeach
Assuming that the $aboutcontents is numeric array just use the good old fashioned for loop instead of your new fangled foreach
// Notice you start at 1 and your first
// elem is 0 so... ta da... skipped
#for ($i = 1; $i < count($aboutcontents); $i++){
$about = $aboutcontents[$i]; //This is the object
//now use $about as you would
}
Note: I have not used Larvel or blades but based on the docs this should be doable
There is two way to do this:
1- if your $key is numeric you can use:
#foreach($aboutcontent as $key => $about)
#if($key == 0)
#continue
#endif
code
#endforeach
2- if a $key is not numeric
use #loop->first as a condition
You need some kind of a counter if you want to do that in blade:
<?php $count = 0;?>
#foreach
#if($count>1)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endif
$count++
#endforeach
EDIT:
I like the answer provided by Mark Baker in the comment better
#foreach(array_slice($aboutcontent, 1) as $about)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endforeach
Alternatively, you can just remove the first element from the array before iterating:
#php
array_shift($aboutcontent);
#endphp
#foreach($aboutcontent as $about)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endforeach
The advantage is that you do not need any conditions to verify you are not the in the first iteration. The disadvantage is that you might need the first element within the same view, but that we don't know from your example.
Note It might make more sense to remove the first element from the array before you pass on the data to the view.
For reference, see:
http://php.net/manual/en/function.array-shift.php
https://laravel.com/docs/5.4/blade#php
Try this
#foreach($aboutcontent->slice(1) as $about)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endforeach

Categories