I hope you are well. I am currently learning Laravel 8, and am of course encountering some small complications while learning. I would like to understand the following:
What does "{{ ... }}" mean? I understand the principle, of putting PHP in HTML, but how does it work? What does the "{{ }}" mean?
What does "#if", "#foreach" mean? I also understand the principle of putting a conditional aspect within a file, but what does "#" represent?
I have a particular problem, and when I search on the internet, I quickly come across PHP version problems (and I confess I don't believe it):
For example, I make a complete pagination system, and when I set up a foreach to set up my page numbers, it returns me an error:
#if ($paginator->hasPages())
<ul class="pagination">
#foreach ($elements as $element)
#if (is_string($element))
<li class="page-item disabled"><span class="page-link">{{ $element }}</span></li>
#endif
#if (is_array($element))
#foreach ($element as $page => $url)
<li class="page-item">
{{ $page }}
</li>
#endforeach
#endif
#endforeach
</ul>
#endif
Now, if I put this:
#if ($paginator->hasPages())
<ul class="pagination">
#foreach ($elements as $element)
#if (is_string($element))
<li class="page-item disabled"><span class="page-link">{{ $element }}</span></li>
#endif
#if (is_array($element))
#foreach ($element as $page => $url)
<li class="page-item">
{{ $page ?? '' }}
</li>
#endforeach
#endif
#endforeach
</ul>
#endif
Now it works properly. If I replace the elements like the first text quote, then it's no longer a problem...
Could it be the cache? From the update of the server pages without restarting it with Laravel 8?
I thank you in advance for your help. I come here as a last resort, without knowing what to write on the internet, without finding a very logical and coherent answer...
Thanks 🙏
Typical example of an error that I have right away:
ParseError
syntax error, unexpected ' ' (T_STRING), expecting ')' (View: /Applications/MAMP/htdocs/Laravel/Site/resources/views/vendor/pagination/custom.blade.php)
In the code, I wrote this:
#if ($paginator->onFirstPage())
#else
<li class="page-item">
<a href="{{ $paginator->previousPageUrl() }}" rel="prev" class="page-link">
Précédent
</a>
</li>
#endif
For it to work I have to put : {{ $paginator->previousPageUrl() ?? '' }}
Blade templates
Those "{{ ... }}" and "#" are just statements by which laravel template engine called blade knows what to do with provided statement.
You can read documentation of blade here.
Blade's {{ }} echo statements are automatically sent through PHP's htmlspecialchars function to prevent XSS attacks.
"{{ ... }}" is called echo statement. So:
{{ $variable }}
is translated to something like this:
<?php echo htmlspecialchars($variable )?>
#if, #for, #foreach are replaced to something like this:
<?php foreach($variable as $key => $value) { ?>
// template that you put inside
<?php } ?>
and so on.
Null coalescing operator
Errors with your functions that you claim that you have to put ?? '' are caused that probably value of function / function itself is null.
The ?? operator checks for nulls (what you can read here):
The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not null; otherwise it returns its second operand.
What is the correct way to call the "Links" function after this "Foreach"?
I don't know how to handle the variable to put in function.
#inject('usuarios', 'App\User')
#foreach($usuarios->getIndicados() as $user)
#endforeach
<div class="row">
<div class="col-12 text-center">
{{ $usuarios->getIndicados()->links() }}
</div>
</div>
Maybe it's just an editing error, but in your output the -tags don't seem to be closed again. Also, there should be no space like < a> at the beginning of the tag. And < a hr_ef= ... is obviously wrong.
In order to style them, you can add a class attribute to the tags while building the string and do the style-stuff in css.
This is what laravel document provides. You need to add links in the collection.
<div class="container">
#foreach ($users as $user)
{{ $user->name }}
#endforeach
{{ $users->links() }}
I have an old database filled with info and now i want to display Category names from that DB and getting this error.
Here is my controller
public function forums(){
$cats = Forum_cats::all();
return view ('lapas.forums.index')->with('cats', $cats);
}
}
here is my view
#if(count($cats >1))
#foreach($cats as $cati)
<div class = "well">
<h3>{{$cati->description}}</hr>
</div>
#endforeach
#else
#endif
and here is screen of DB structure
http://prntscr.com/mg5nk1
Ask for more info if needed!
It looks like your operator is misplaced:
#if(count($cats) > 1)
#foreach($cats as $cati)
<div class = "well">
<h3>{{$cati->description}}</hr>
</div>
#endforeach
#else
#endif
It looks like you're trying to loop through these $cats in a Blade template. You might also try forelse:
#forelse($cats as $cati)
<div class = "well">
<h3>{{$cati->description}}</hr>
</div>
#empty
{{-- Action if there are none --}}
#endforelse
Edit: Docs here: https://laravel.com/docs/5.4/blade#loops
Your if is wrong. Try :
#if($cats->count() > 1)
I wanted to check if variable is there or not in blade ..for that i have used following lines:
#if(is_null($products))
<div class="alert alert-warning">
<strong>Sorry!</strong> No Product Found.
</div>
#else
#foreach($products as $product)
//
#endforeach
#endif
The problem is when there is $products on blade I could show inside of foreach loop but when i get empty variable.I couldn't show the message No Data Found instead it shows only empty space?
is there any problem of checking variable inside of blade?
Controller code :
public function productSearch(Request $request)
{
$name = $request->name;
$products = Product::where('name' , 'like', '%'.$name.'%')->get();
return view('cart.product',compact('products'));
}
I generally use PHP count() :
#if(count($products) < 1)
<div class="alert alert-warning">
<strong>Sorry!</strong> No Product Found.
</div>
#else
#foreach($products as $product)
//
#endforeach
#endif
You may also check with PHP empty() like :
#if(!empty($products))
As you can see in the documentation :
#forelse ($users as $user)
<li>{{ $user->name }}</li>
#empty
<p>No users</p>
#endforelse
This code will allow you to parse all the users and display a list of them. if the $users variables is empty, then it will display a paragraph
so for you :
#forelse ($products as $product)
//
#empty
<div class="alert alert-warning">
<strong>Sorry!</strong> No Product Found.
</div>
#endforelse
As of Laravel 5.7, You can also do this:
#empty(!$products)
<div class="alert alert-warning">
<strong>Sorry!</strong> No Product Found.
</div>
#endempty
You can check like
#if(isset($products) && !empty($products))
<div class="alert alert-warning">
<strong>Sorry!</strong> No Product Found.
</div>
#else
#foreach($products as $product)
//
#endforeach
#endif
What about checking length?
#if(count($products)) >= 1)
#foreach($products as $product)
//
#endforeach
#else
<div class="alert alert-warning">
<strong>Sorry!</strong> No Product Found.
</div>
#endif
Because empty set (i mean a data stucture with zero elements) is not null at all.
php > $a = [];
php > echo is_null($a) ? 1 : 0;
// => 0
is_null Finds whether the given variable is NULL or not. but in our case we need to check whether the value in empty or not for this you can use either isset() or empty() function both work same in your case
while isset — Determine if a variable is set and is not NULL and
empty — Determine whether a variable is empty and also tell variable is set
#if(isset($products) && !empty($products))
#foreach($products as $product)
//
#endforeach
#else
<div class="alert alert-warning">
<strong>Sorry!</strong> No Product Found.
</div>
#endif
Do this,
Check is there any records "->count() > 0" then do foreach,
else alert.
#if ($products->count() > 0 )
#foreach($products as $product)
//enter code here
#endforeach
#else
<div class="alert alert-warning">
<strong>Sorry!</strong> No Product Found.
</div>
#endif
For me I will use logic like this
if(!$products->isEmpty()){
return view('cart.product', compact('products'));
}else{
return view('pageerror', compact('products'));
}
then you can call pageerror from your view folder to display any page that does not has data
#forelse($products as $product)
<p>do some thing</p>
#empty
<p>No Products</p>
#endforelse
Refer
Try this
#forelse($products as $product)
//
#empty
<div class="alert alert-warning">
<strong>Sorry!</strong> No Product Found.
</div>
#endforelse
I found the most effective (and by far the easiest way) of accomplishing what you're trying here to be as follows.
Assumption #1: You Know The Variable Exists Within The View.
REMEMBER: an empty array will always return false.
Therefore, there is no real need to run it through a function like empty or is null.
Comparing it to null will tell you if it exists or not.
(You could by-pass this assumption by checking to see if the variable is not equal to NULL (it's kind of bloaty if you've passed that variable through to the view, so in my opinion, I would KEEP IT SIMPLE STUPID [KISS] - if you want, you can go all fancy later when it comes to further refactoring).
ANYWAY..
I would stick to pretty similar code as you have now, maybe something like this here would be the code for your view:
#if(!$products)
<div class="alert alert-warning">
<strong>Sorry!</strong> No Product Found.
</div>
#else
#foreach($products as $product)
// {{ $product . “code goes here.” }}
#endforeach
#endif
and the code for your controller would look something like this (you almost had it, remember: "perfect practice makes perfect!" - but yeah, the controller code:
public function productSearch(Request $request)
{
// Easily obtain the name submitted by the form (I assume via the request object
// dependency injection magic
$name = $request->name;
// I would consider using the DB builder tool, as follows, as there is more docs on it
// see: https://laravel.com/docs/5.6/queries - this will return a collection (iterable)
$products = DB::table(‘products’)
->where('name' , 'like', '%'.$name.’%’)
->get();
// simply passing to the view
return view('cart.product', compact('products'));
}
You would also need to include the Product model, DB (Laravel) and (as per usual) the request object, as follows:
// Laravel Dependencies
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
// User Created Model
use App\Product;
Hopefully, this has been helpful!
I'm using Blade templating with Laravel and I'm trying to use a #foreach loop to display notifications. The problem is that if I have say 10 notifications, the first notification is repeated 10 times.
The code to output each notification:
#foreach ( Auth::user()->unreadNotifications as $notification )
{{ $notification->type->web_template }}
{{ $notification->id }}
#include($notification->type->web_template)
#endforeach
web_template will output a path to the template: notifications.web.user_alert
For each iteration of the loop the {{ $notification->type->web_template }} and {{ $notification->id }} will output what they're supposed to but #include($notification->type->web_template) will only output the first notification each time.
So the output will look like:
156 notification.web.new_message
You have a new message from Joe.
154 notification.web.user_alert
You have a new message from Joe.
145 notification.web.new_like
You have a new message from Joe.
I think it's some sort of cache issue maybe, but I couldn't find anyone with the same problem.
Any ideas?
UPDATE: Adding some code
Example notification view:
#extends('layouts.notification-wrapper')
#section('url', url('jobs/'.$notification->job_id.'#highlight'.$notification->bid_id))
#section('image', '/assets/img/no-photo.jpg')
#section('header', $notification->collector->firstname . ' ' . $notification->collector->secondname)
#section('description')
has placed a bid of €{{ number_format($notification->bid()->withTrashed()->first()->amount,0) }} on your job.
#stop
Notification wrapper:
<li #if(!$notification->read)
class="unread"
#endif>
<a href="#yield('url')" data-id="{{ $notification->id }}">
<div class="pull-left">
<img src="#yield('image')" class="img-circle" alt="user image">
</div>
<h4>
#yield('header')
<small><i class="fa fa-clock-o"></i> {{ $notification->created_at->diffForHumans()}}</small>
</h4>
<p> #yield('description')</p>
</a>
</li>
Answering my own question!
Found this: Laravel Blade Templates Section Repeated / cache error
Basically whatever way it works I need to overwrite my sections when looping and using #yield... I think. So I need to replace #stop with #overwrite in my views.