在视图laravel中显示空值的变量
我试图从控制器中的函数向视图发送数据,但变量视图未显示任何结果在视图laravel中显示空值的变量,laravel,Laravel,我试图从控制器中的函数向视图发送数据,但变量视图未显示任何结果 public function goedit($id) { $catg = Category::where('cat_id',$id)->first(); return view('product.gocatedit')->with('row', $catg); } 我的观点是 @if(isset($row)) <form action="{{action('P
public function goedit($id)
{
$catg = Category::where('cat_id',$id)->first();
return view('product.gocatedit')->with('row', $catg);
}
我的观点是
@if(isset($row))
<form action="{{action('ProductController@edit')}}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="col-sm-12">
<h1 style="text-align:center;">Edit Items</h1>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Category</th>
<th>Item</th>
<th>Price</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<input type="hidden" name="item_id" value="{{ $row->item_id}}">
<td><input class="form-control" name="item_name" value="{{$row->item_name}}" /></td>
<td><input class="form-control" name="item_price" value="{{$row->item_price}}" /></td>
<td><input class="btn btn-primary btn-block btn-flat" type="submit" value="Edit"></td>
</tr>
</tbody>
</table>
</div>
</form>
@endif
@if(isset($row))
编辑项目
身份证件
类别
项目
价格
@恩迪夫
请帮助,谢谢您的查询似乎返回了
null
,这就是问题所在(数据库中没有这样的ID)
如果要检查null
,请使用is\u null
:
@if(is_null($var))
或者您可能想使用空:
@if(!empty($row))
实际上应该是:
public function goedit($id)
{
$catg = Category::findOrFail($id);
return view('product.gocatedit')->with('row', $catg);
}
然后,您似乎没有使用默认主键,即“id”,因此在您的模型中添加
public class Category{
...................
protected $primaryKey = "cat_id";
.....................
}
如果将->与('something','data')
一起使用,则传递的数据将在会话('something')
中,其目的是“刷新”消息
您需要这样做(视图数据应该作为数组传递,并作为view()
函数的第二个参数):
请看这个。它应该可以帮助你。不,如果在函数中显示结果,那么它的bcoz值不为null($row)。你能发布dd($row)的结果吗代码>请?第{#228项▼ #可填充:数组:4[▶] #primaryKey:“item_id”#连接:null#表:null#每页:15+递增:true+时间戳:true#属性:数组:6[▼ “商品id”=>“1”“商品名称”=>“中国面条”“商品价格”=>“123”“猫名”=>“中文”创建于“=>”2016-04-09 11:02:15”“更新于“=>”2016-04-14 06:37:54”];原件:数组:6[▶] #关系:[]隐藏:[]可见:[]附件:[]受保护:数组:1[▶] #可观察对象:[]#with:[]#morphClass:null+存在:true+最近创建:false}您是否在视图中运行了dd()
?如果$row
有正确的数据,那么哪个变量是null
?实际上我正在尝试将变量发送到调用该函数的视图,并尝试在模式上显示它。
public class Category{
...................
protected $primaryKey = "cat_id";
.....................
}
public function goedit($id)
{
$catg = Category::where('cat_id',$id)->first();
return view('product.gocatedit', ['row' => $catg]);
}