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
Related
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.
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.
I'm having a problem with #if syntax.
I want to show some message but it's doesn't work.
It works only on one type. but I need in 3 types.
#if($article->type == 'analytic', 'news', 'interview')
SHOW THIS
#endif
You have to use like below
#if (in_array($article->type, array("analytic", "news", "interview")))
SHOW THIS
#endif
Update
Array can be define also like this which will short your code
["analytic", "news", "interview"]
First, I think you should probably learn programming as a whole first, then start using Laravel.
If what you want is to show users a certain content based on if the $article->type is one of the values you listed i.e. ["analytic", "news", "interview"];
You can do this:
#if (in_array($article->type, ["analytic", "news", "interview"]))
SHOW THIS
#endif
There are couple of other alternatives as well, but the above is the most performant solution off the top of my head.
I have a mcqs website, the problem is that when i try to load a quiz page with multiple questions and their answers, it loads fine.
Working code:
#foreach ($question->answers as $answer)
<span style="display:inline-block">
{{$answer->content}}
</span>
#endforeach
Not Working Code:
#foreach ($question->answers as $answer)
<span style="display:inline-block">
{!! $answer->content !!}
</span>
#endforeach
{!! $answer->content !!} is the same as echo $answer->content
however for the same code when i try to render the answers with HTML formatting using <?php echo , the page stops rendering midway. forexample if say the page was to load 30 questions , it will only load 10 and stops rendering . Even when i tried using {!!$answer->content!!} , the problem remains the same.
Note: i am returning set of questions using the below code:
$questions = Question::all()->where('exam_id',$exam_id)->random($questionsCount)->shuffle();
return view('quiz',compact('exam','questions','questionsCount','t'));
I have one to many relationship setup so i can extract the answers of the question using $question->answers
it works fine when i don't echo any answer content.
Anyhelp will be greatly appreciated. Thanks!
Looks like an issue with unescaped text. When you use the blade template's escape method ({{ }}) it is successful. When you use the unescaped method, ({!! !!}) it fails. This is the same when using the unescaped php echo.
There is likely some type of character in one of your questions that is causing a break of the loop or php echo.
To fix, use the blade escape {{ }} or php escape (htmlentities(), htmlspecialchars() etc) in the echo.
htmlentities-vs-htmlspecialchars is worth checking out as well.
A couple of days ago, I decided to start using laravel for the next project, but I'm confused as I don't find the documentation very compelling and I'm still a laravel beginner .
So, I didn't find a solution for how to create a layout using PHP (and not built in blade templating engine).
How can I do that? What's the best way to organize layouts in a big project?
Thank you
There are many methodologies that deal with handling templates.
Here are few,
1. Using a regular include or require
You can include the header.php , sidebar.php and footer.php and as many files that you prefer for each sector(It depends on the size of the template)
2. Using a common file and having classes inside it
Include a single file and call the classes to render each area
like
class Head {
public function render($_page, $_data) {
extract($_data);
include($_page);
}
}
3. Use a Templating Engine
You shall prefer few templating engine like smart, raintpl etc., (I guess you don't prefer it ;) )
4. Acquiring by inc
You can include as suggested here
<html>
<head>
<title><?=$this->title</title>
</head>
<body>Hey <?=$this->name?></body>
</html>
And the php area would be
$view = new Template();
$view->title="Hello World app";
$view->properties['name'] = "Jude";
echo $view->render('hello.inc');
5. By having template segments in db
Believe me, I saw many good sites which stores the template in the database and it will be rendered each time. It might look like strange idea, but even i tried it for one of my project.
Conclusion :
But if i use Laravel, for sure i will prefer the Blading Tempalte Engine and I recommend you the same.
Update :
Few benefits of Using Blade Templates
1. Easy Setting of attributes
Set the attributes on the go
<title>App Name - #yield('title')</title>
2. Easy yielding
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
3. Simple echoing
Like this
Hello, {{ $name }}
4. Easy Condition
Like this
{{ isset($name) ? $name : 'Default' }}
5. Never Escape
Like this
Hello, {!! $name !!}.
6. Beautiful If Statements
I prefer this way to make my code more beautiful
#if (count($records) === 1)
I have one record!
#elseif (count($records) > 1)
I have multiple records!
#else
I don't have any records!
#endif
7. Checking Authentication
The simplest way to check the authentication
#unless (Auth::check())
You are not signed in.
#endunless
8. Easy For Loop
How this for loop looks like
#for ($i = 0; $i < 10; $i++)
The current value is {{ $i }}
#endfor
9. Awesome foreach statement
Splitting the key and value can't be more easy than this
#foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
#endforeach
10. Include the files
How about include file like this
#include('view.name')
11. Passing parameters to views
Can Pass this array to your view
#include('view.name', ['some' => 'data'])
Source : Laravel Templates