我想检查刀片中是否存在变量..因为我使用了以下行:
@if(is_null($products))
<div class="alert alert-warning">
<strong>Sorry!</strong> No Product Found.
</div>
@else
@foreach($products as $product)
//
@endforeach
@endif
问题是当刀片上有$products
我可以在foreach
循环内显示但是当我得到空变量时。我无法显示消息No Data Found
而不是它只显示空的空间?
检查刀片内的变量有什么问题吗?
控制器代码:
public function productSearch(Request $request)
{
$name = $request->name;
$products = Product::where('name' , 'like', '%'.$name.'%')->get();
return view('cart.product',compact('products'));
}
答案 0 :(得分:4)
我通常使用PHP count():
@if(count($products) > 0)
<div class="alert alert-warning">
<strong>Sorry!</strong> No Product Found.
</div>
@else
@foreach($products as $product)
//
@endforeach
@endif
您也可以使用PHP empty()查看:
@if(!empty($products) > 0)
答案 1 :(得分:2)
检查长度怎么样?
@if(count($products)) >= 1)
@foreach($products as $product)
//
@endforeach
@else
<div class="alert alert-warning">
<strong>Sorry!</strong> No Product Found.
</div>
@endif
因为空集(我的意思是具有零元素的数据结构)根本不为空。
php > $a = [];
php > echo is_null($a) ? 1 : 0;
// => 0
答案 2 :(得分:1)
对我来说,我会使用这样的逻辑
if(!$products->isEmpty()){
return view('cart.product', compact('products'));
}else{
return view('pageerror', compact('products'));
}
然后你可以从你的视图文件夹中调用pageerror来显示任何没有数据的页面
答案 3 :(得分:1)
@forelse($products as $product)
<p>do some thing</p>
@empty
<p>No Products</p>
@endforelse
答案 4 :(得分:1)
您可以查看
@if(isset($products) && !empty($products))
<div class="alert alert-warning">
<strong>Sorry!</strong> No Product Found.
</div>
@else
@foreach($products as $product)
//
@endforeach
@endif
答案 5 :(得分:1)
试试这个
<script src="https://checkout.stripe.com/checkout.js"></script>
<form id="payment-form" method="post" action="procedures/charge.php" >
<input id="monthly" type="radio" name="plan-id" value="monthly1" > $10/month<br>
<input id="annual" type="radio" name="plan-id" value="annual1" > $89/year<br>
<button type="submit" id="customButton">Sign Up</button>
</form>
<script>
document.getElementById('customButton').addEventListener('click', function(e) {
//This is where I'm having my problem
var plan = document.querySelector('input[name = "plan-id"]:checked').value;
if(plan = 'monthly1') {
var description = 'Monthly Subscription';
var amount = 1000;
}else if(plan = 'annual1') {
var description = 'Annual Subscription';
var amount = 8900;
}
// Open Checkout with further options:
handler.open({
name: 'Your Strength Coach',
description: description,
amount: amount,
email: '<?php echo $user['email'] ?>',
});
e.preventDefault();
});
</script>
答案 6 :(得分:1)
is_null查找给定变量是否为NULL。但在我们的情况下,我们需要检查是否为空的值,你可以使用isset()或empty()函数在你的情况下是否相同
whileset - 确定变量是否设置且不是NULL和
empty - 确定变量是否为空并且还设置变量
@if(isset($products) && !empty($products))
@foreach($products as $product)
//
@endforeach
@else
<div class="alert alert-warning">
<strong>Sorry!</strong> No Product Found.
</div>
@endif
答案 7 :(得分:1)
正如您在文档中看到的那样:
@forelse ($users as $user)
<li>{{ $user->name }}</li>
@empty
<p>No users</p>
@endforelse
此代码允许您解析所有用户并显示它们的列表。如果$users
变量为空,则会显示段落
所以对你:
@forelse ($products as $product)
//
@empty
<div class="alert alert-warning">
<strong>Sorry!</strong> No Product Found.
</div>
@endforelse
答案 8 :(得分:1)
这样做, 检查是否有任何记录“-> count()> 0”,然后执行foreach, 其他警报。
@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
答案 9 :(得分:0)
从Laravel 5.7开始,您还可以执行以下操作:
friend
答案 10 :(得分:0)
我发现完成您在此处尝试的工作最有效(也是迄今为止最简单的方法)。
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.
(您可以通过检查变量是否不等于NULL来绕过此假设(如果您已将该变量传递给视图,则有点肿,所以我认为KEEP IT SIMPLE STUPID [KISS]
-如果您愿意,可以在以后进行进一步重构时花点时间。)
无论如何。
我会坚持使用与现在非常相似的代码,也许像这样的代码将成为您视图的代码:
@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
,控制器的代码应如下所示(您几乎已经掌握了,请记住:"perfect practice makes perfect!"
-但是,是的,控制器代码:
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'));
}
您还需要包括产品模型,DB(Laravel)和(通常)请求对象,如下所示:
// Laravel Dependencies
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
// User Created Model
use App\Product;
希望这对您有所帮助!