Laravel PHP If/Else in Blade Doesn't Work - laravel

Laravel PHP fans, Any idea why this renders both the actual partial view and the No Data Found as well? And interestingly, it renders No Data Found first. If I do a dump and die in the if condition, I get that dd. If I put it in the else condition, I get it there. It must be that the page starts rendering before the data for regionLevel actually comes back. Then when it does it dutifully prints it out. Is there any way around this weirdness?
#if(isset($regionLevel) && count($regionLevel) > 0)
<div class="size-wrapper">
#include('manager/reports/order-data/sections/partials/region-partial')
</div>
#else
<div class="row">
<div class="col-12">
<div class="d-flex justify-content-left m-t-5 m-b-5 m-l-10">No data found.</div>
</div>
</div>
#endif

Related

Viewing all photos of post using UIKIT using Lightbox Panel Options "items"

I am using UIKIT Lightbox to display post images like Facebook as below:
Controller
$postImages = postsImages::where('postId', $post->postId)->limit(3)->get();
Blade
<div class="uk-child-width-1-3#m" uk-grid uk-lightbox="animation: slide">
#foreach ($postImages as $postImage)
<div>
<a class="uk-inline" href="{{$postImage->imageLink}}">
<img src="{{$postImage->imageLink}}">
#if ($loop->last && $post->hasImage > 3)
<div class="uk-overlay-default uk-position-cover more">
<div class="uk-position-center">
<h1>+{{$post->hasImage - 3}}</h1>
</div>
</div>
#endif
</a>
</div>
#endforeach
</div>
I limited the image thumbnails to 3 and the rest will be shown as +number_of_remaining_photos same like Facebook.
The problem is that when I click on a thumbnail to make it large and view the rest then I can see only those three images which I limited.
I went through UIKIT lighbox documentation and I found items option but I don't know how to use it in order to view all the images of that post by creating another query like below:
$postImages = postsImages::where('postId', $post->postId)->get();
to get all images there and showing it in the lightbox.
P.S. I am not sure, that is item option for that purpose or not. )
This is a practical solution.
1- Make another query without limit.
$postImagesAll = postsImages::where('postId', $post->postId)->get();
2- Add data-uk-lightbox="{group:'my-group'}" attribute to the result of both queries like below and everything will work fine:
<div class="uk-grid-collapse" uk-grid uk-lightbox="animation: slide;">
#foreach ($postImages as $postImage)
<div>
<a class="uk-inline" href='/storage/posts/{{$postImage->imageLink}}' data-uk-lightbox="{group:'my-group'}">
<img src="{{$postImage->imageLink}}"/>
#if ($loop->last && $post->hasImage > 3)
<div class="uk-overlay-default uk-position-cover more">
<div class="uk-position-center">
<h1>+{{$post->hasImage - 3}}</h1>
</div>
</div>
#endif
</div>
</a>
</div>
#endforeach
#foreach ($postImagesAll as $postImage)
<a class="uk-hidden" href="{{$postImage->imageLink}}" data-uk-lightbox="{group:'my-group'}"></a>
#endforeach
</div>

How do I use a row count in an #if in a Laravel view?

I am brand new to Laravel and I'm running Version 6.
I want my view to display a button if one of my MySQL tables has rows that meet a specific condition but I'm having trouble figuring out how to code it - and even WHERE to code it - within my Laravel application.
My MySQL table is called diary_entries and various users of the system will contribute zero to n rows to it. Each row of the table contains a user id called client. When a given user goes to the Welcome view, I want the view to determine if that user currently has any rows in the diary_entries table. If he does, I want to display a button that will take him to another page where the entries can be displayed or edited or deleted.
I think I want to construct an #if that counts the number of records for that user; if the count is greater than zero, I want to display the button, otherwise the button is not displayed.
The problem is that I can't figure out how to code this. I've looked at the examples in the Eloquent section of the manual but they aren't particularly clear to me. I found a note near the top that said the count() function expects a Collection as an argument and that the result of an Eloquent statement is always a Collection so I guessed that I just have to execute an Eloquent query, then apply count() to the resulting Collection. But every variation of that idea which I've tried has thrown exceptions.
Here was the guess that seemed most logical to me:
#extends('layout');
#section('content');
<div class="content">
<img class="centered" src="/images/sleeping-cat.jpg" alt="sleeping cat" height="250">
<div class="title m-b-md">
<h1> Sleep Diary </h1>
</div>
<div>
<h3>{{Auth::user()->name }}</h3>
</div>
<div>
#if (count(App\DiaryEntry::select('*')->where('client', Auth::user()->name) > 0))
<p>
<a class="btn btn-primary"> View / edit existing sleep diary entries </a>
</p>
#endif
</div>
<div>
<p>
<a class="btn btn-primary" href="/diaryEntries"> Create a new sleep diary entry </a>
</div>
</div>
#endsection
This is obviously wrong because it throws an exception so how do I make it right? Does the building of the collection have to move into the Controller? If so, how do I invoke the method and see its result? Or can I do something like I have already done but just adjust the syntax a bit?
EDIT
I've imitated Sehdev's suggestion but I get this error:
$count is undefined
Make the variable optional in the blade template. Replace {{ $count }} with {{ $count ?? '' }}
Here is my welcome view:
#extends('layout');
#section('content');
<div class="content">
<img class="centered" src="/images/sleeping-cat.jpg" alt="sleeping cat" height="250">
<div class="title m-b-md">
<h1>Sleep Diary</h1>
</div>
<div>
<h3>{{ Auth::user()->name }}</h3>
</div>
<div>
#if ($count) > 0))
<p>
<a class="btn btn-primary">View/edit existing sleep diary entries</a>
</p>
#endif
</div>
<div>
<p><a class="btn btn-primary" href="/diaryEntries">Create a new sleep diary entry</a>
</div>
</div>
#endsection
And this is the relevant function from DiaryEntryController:
public function countEntriesOneUser()
{
$count = DiaryEntry::select('*')->where('client', Auth::user()->name)->count();
view("welcome", compact("count"));
}
Should the compact function be returning $count instead of count? I can't find the compact function in the manual with the search function so I'm not clear what it does or what the proper syntax is. I just tried changing the last line of the function to
view("welcome", $count);
but that produced the same error.
Try this,
#php
$count=\App\DiaryEntry::where('client', Auth::user()->name)->count();
#endphp
#if($count>1)
<p><a class="btn btn-primary">View/edit existing sleep diary entries</a></p>
#endif
Using App\DiaryEntry::select('*')->where('client', Auth::user()->name) directly on your blade template is a bad practise.
You can execute your question in your controllers method and then pass the result on your view file
Your function
function test(){
$count = DiaryEntry::select('*')->where('client', Auth::user()->name)->count(); // get the total no of records using count
view("index", compact("count")) // pass your count variable here
}
then you can directly use $count in your #if condition
Your blade template
<div>
#if ($count > 0)
<p><a class="btn btn-primary">View/edit existing sleep diary entries</a></p>
#endif
</div>

Extension doesn´t work after deleted Cache

i have a problem with one of my extensions i created with Extensionbuilder.
After i deleted the Cache there is no content anymore!
<f:flashMessages renderMode="div"/>
<div class="tx-camping-list">
<f:for each="{offers}" as="offer" iteration="it">
<div class="content-list-item">
<div class="contentLeft floatLeft">
<f:render partial="BaseItem/ListImages" arguments="{item:offer}" />
<div class="wrap-right floatRight">
<h3>
<f:link.action arguments="{offer:offer}">
<f:format.raw>{offer.names}</f:format.raw>
</f:link.action>
</h3>
<h4>
<f:format.raw>{offer.teaser}</f:format.raw>
</h4>
<div class="description"><f:format.crop maxCharacters="240"><f:format.html>{offer.description}</f:format.html></f:format.crop>
<p>
<f:link.action arguments="{offer:offer}">
<f:translate key="read_more" default="Read more" /> >
</f:link.action>
</p>
</div>
</div>
</div>
<div class="roomPrice floatRight">
<f:render partial="Price/ListPriceBox" arguments="{item:offer}" />
<f:render partial="BaseItem/Button" arguments="{item:offer,type:'Offer'}"/>
</div>
<div class="clearFloat"></div>
</div>
</f:for>
</div>
The image, {offer.names}, {offer.teaser} etc. is NULL, BUT the link {offer:offer} is working
When i reload the page the first time after i deleted the Cache i get the error:
The argument “each” was registered with type “array”, but is of type “string” in view helper
After a reload everything works fine except this plugin!
Maybe some of you can help me
Thanks
Use <f:debug> to find out what you real values are. You cannot loop over a string. Most likely, you are mistaken over your nesting level of your object.
Also do not use <f:format.raw> unless you already ensured, that the contend is htmlspecialchar'd. Otherwise you loose the XSS protection.
This can just fix the error if its caused by a missing reference on records
You could try to add the following line of code to your setup.ts (or .txt; depending on configuration and personal preferences):
File: slider/Configuration/TypoScript/setup.ts:
plugin.tx_slider.persistence.storagePid = 15
Note: replace tx_slider with your extensionkey and the 15 with the pagenumber where your records (in this case your offer-records) are located

Laravel 4 View Nesting Main Layout Subviews Content and Sidebar

This is my first post so please be gentle with me.
I have set my master layout to call "main"
<div class="row">
{{$main}}
</div>
and I have a view called home.blade
<div class="col-md-9">
<div class="content">
{{$content}}
</div>
</div>
<div class="col-md-3">
<aside class="sidebar">
{{$sidebar}}
</aside>
</div>
and in my controller I have:-
$this->layout->main = View::make('home')->nest('content', 'property.viewmany', compact('properties'));
This works for the content variable, but at this point I'm stumped at how to get the sidebar variable to work. Is there a way to nest the two variables into the home.blade from the controller?
I've attempt various, albeit, shots in the dark, but I always get either content not defined or sidebar not defined.
You can put a first variable in the first view like this:
$this->layout->main = View::make('home', compact('sidebar'))->nest('content', 'property.viewmany', compact('properties'));

for-loop interferes with others in web.py

I'm a newbie in web.py.
In my template script exists two FOR loops,it seems that the former(code1) interferes the later one(code2).Each one of them displays well alone,but abnormal when they are in one template script.
code1
$for data in posts:
<div id="title">
<h2>$data.title</h2>
</div>
<div id="marker">
<p>
User
$data.post_on $data.catalog
</p>
</div>
<div id="content">
<p>$data.content</p>
</div>
code2
$for (cat,ctr) in catcollector(posts).items():
<ul>
<li>$cat ($ctr)</li>
</ul>
I'm confused with this phenomenon.Does any one have idea about this? Thanks in advance~
I'm not sure this is your only problem, but it looks like your indentations are not correct. Things within the for loop should be indented.

Resources