Query data in database to show posts - php

How to foreach 5 posts orderBy DESC show 2 type css, 5 posts from the same category

okay maybe this question not clear but i try to figure out what you want.you can filter what content will show by just add some condition in your loop
#foreach($news as $key => $new)
#if($key < 2)
//div with big image class
#else
//div with small image class
#endif
#endforeach

Laravel Blade allows to use a helpful set of variable like
Loop Variable
When looping, a $loop variable will be available inside of your loop. This variable provides access to some useful bits of information such as the current loop index and whether this is the first or last iteration through the loop:
#foreach($news as $new)
#if($loop->index < 2)
//div with big image class
#else
//div with small image class
#endif
#endforeach

Related

Laravel Blade simple foreach including $loop->first

I have a form where I display four text field inputs. The current app()->getLocale() input is displayed on the left, and the below code is for the remaining 3 locales which are displayed on the right:
#foreach(['ca','en','es','nl'] as $lang)
#if(app()->getLocale() == $lang) #continue #endif
<li>
<a href="#{{ $lang }}" class="#if($loop->first) active #endif"
</li>
#endforeach
These are all menu tabs that are hidden, only the first one should be displayed as active, hence:
#if($loop->first) active #endif
The problem however is, that when the current locale is ca, also the $loop->first() will be ca. And this one cannot be the active one, since it will never be displayed on the right side.
I am trying to find an easy fix without too many if else things. Also, the array ['ca','en','es','nl'] will be changed for some data that comes from the config, so there will be more locales later and ca will not always be the first one. So I cannot do checks with #if(app()->getLocale() == 'ca') since that will change in the future as well.
Instead of:
['ca','en','es','nl']
with:
array_diff(['ca','en','es','nl'], [app()->getLocale()])
and remove this:
#if(app()->getLocale() == $lang) #continue #endif
This will remove the item representing the current language from your array.

Attempt to read property "image" on bool in Laravel

I am new to Laravel Plz help me to solve this problem.
I have multiple images of a single product in the Database and want to show them in a carousel in foreach loop.
i also have the product id and images in a database table.
My Function
public function view_product_details($id)
{
$product_details_images = product_attr_images::where('product_id', $id)->first();
// dd($product_details_images);
return view('frontend.view_product_details', compact( 'product_details_images'));
}
My View File
<div class="carousel-inner border">
#foreach ($product_details_images as $key=> $images)
<div class="carousel-item{{$key==0 ? 'active' : ''}} ">
<img class="w-100 h-100" src="product/{{$images->image}}" alt="Image">
</div>
#endforeach
</div>
I want to fetch all images and show them in the carousel in foreach loop but I can't. i am facing error of images in bool. I don't know what is happening.
plz help me. I will be very thankful to you
Please follow Laravel's naming conventions. They are not only easier for us to read, but also used by Laravel to perform to many magical operations under the hood.
You see one image because you used first() method which returns the first matched instance. In order to fetch all matched instances in a collection, use get().
product_attr_images::where('product_id', $id)->get();
I didn't understand your desing. Your page will only have the images, and display them in carousel, right? If this is the case, your blade file should work, but you should add a space between carousel-item and {{$key==0. If this isn't the case, you should pass the images as a part of a bigger data set from controller to the product page.

Keep foreach loop variable value inside html I tag is not working

i am trying to keep foreach loop variable value inside i tag class..
#foreach($assignment_operators as $assign_operator )
<div style="font-size: 48px;color: white;">{{$assign_operator->ao_code}} </div>
#endforeach
i am getting the value like `
instead of this i am trying to get the image(x) of that i tag class..`
How can i get that i font class.
if i make outside of the foreach i am getting image properly..where did i miss it..please some one help me..

If loops with DB Queries in Laravel Blade Templating

I'm trying to create a continous system for an online magazine's website that I'm building. I want to obviously display artwork differently than I do with other pieces (say a poem or an essay), but I'm having trouble composing the loop that works with both of them. Here's the situation:
I have a $current_genre variable that tells me which genre is currently being read (this is all done via an admin panel). As below, the loop is supposed to identify what the current genre is and then display a certain "tile" accordingly:
#foreach ($pieces as $piece)
#if ($current_genre == 'Artwork')
<div>
<p>Display for images</p>
</div>
#else
<div>
<p>Display for other items</p>
</div>
#endif
#endforeach
I'm not sure what the problem is, as this looks fine to me. It certainly isn't the controller, because I echo the $current_genre variable in other places throughout the page, so I'm not sure what's going on.
EDIT for clarification: The main problem is that no matter what I do, the artwork always appears as a regular piece should, and never as I had it set under the #if($current_genre == 'Artwork') section
Thanks in advance for any help.
Assuming genre is the property of $peice, you can implement the loop as:
#foreach($pieces as $piece)
{
#if($piece->genre == 'Artwork')
** Some Code **
#else
** Some Other Code **
#endif
}
#endforeach
If this does not answer your question,
you could make the question clearer by showing how you are getting the $current_genre variable.
$current_genre is not defined how can you compare like that #if ($current_genre == 'Artwork') you have to access the #if ($piece->current_genre == 'Artwork') like that. hope this will help you

Modify a laravel blade variable without displaying it

In my blade code, I need a counter, to give divs that are rendered by a foreach loop unique id's. For that purpose I created a variable in my blade template like this:
{{ $counter = 0 }}
I use it in the html by just outputting it with {{ $counter = 0 }}
and the later on, I increment it like this: {{ $counter++ }}
It all works like a charm, except that at {{ $counter++ }} it's not only incrementing the variable, it's also outputting it to the view.
is there any way to prevent this?
At first adding logic on blade templating is a bad practice, but sometimes we are forced to do, in that case you can just use PHP tags for it like this:
<?php $counter++; ?>
Another way to do it on Laravel 5.4 as docs indicate:
In some situations, it's useful to embed PHP code into your views. You
can use the Blade #php directive to execute a block of plain PHP
within your template:
#php
$counter++;
#endphp
If you have your positions on the array keys you can use it on the #foreach like this:
#foreach ($variable as $key => $value)
//current position = $key
# code...
#endforeach
I use this extension do set variables on blade:
https://github.com/RobinRadic/blade-extensions
You can use simple as #set($count, 1) or any value you want.

Categories