php - 在刀片 laravel 5.6 中找不到使用控制器传递的数据
问题描述
我创建了一个 laravel 项目。我使用 laravel 5.6
用下面的代码在 balde 中显示产品和类别(产品收到正确。):
<div id="orderProductSelect" class="row">
<h2>chouse product :</h2>
@foreach($products as $product)
<div class="col-md-4" style="margin: 10px;">
<div id="{{ $product->id }}" class="orderProduct" style="cursor: pointer;">
<img style="width: 100%;height: 225px;" src="/images/{{$product->filename}}">
<span style="display: block;">{{ $product->name }}</span>
</div>
</div>
@endforeach
</div>
<div class="categoryAjax" style="display: none;">
<div class="row">
<h3>chouse category :</h3>
@foreach($categories as $category)
<div class="col-md-4" style="margin: 10px;">
<div id="{{ $category->id }}" class="orderProduct" style="cursor: pointer;">
<img style="width: 100%;height: 225px;" src="/images/{{$category->filename}}">
<span style="display: block;">{{ $category->name }}</span>
</div>
</div>
@endforeach
</div>
</div>
并使用 Ajax 发送产品 ID:
jQuery('.orderProduct').click(function(e){
var productId = $(this).attr('id');
var token = '{{ csrf_token() }}';
e.preventDefault();
jQuery.ajax({
url: "{{ url('order/getCategoryAjax') }}",
method: 'post',
data: {
id: productId,
_token: token
},
success: function(data){
$('#orderProductSelect').hide();
$('.categoryAjax').show();
}});
});
我在订单控制器中获得产品 ID。现在找到产品类别:
public function getCategoryAjax(Request $request){
$product = Product::findOrFail($request->id);
$categories = $product->category()->get();
if ($request->ajax()) {
return View::make('user.profile.order.create')->with(compact('categories'))->render();
}
}
我的产品型号:
class Product extends Model{
protected $fillable = [ 'name', 'filename', 'description'];
public function category(){
return $this->hasMany(Category::class,'product_id');
}}
现在我在 create.blade.php 页面中有错误:
Undefined variable: categories (View: /home/laravel-projects/resources/views/user/profile/order/create.blade.php)
解决方案
您需要在此函数中返回响应,而不是整个视图
public function getCategoryAjax(Request $request){
$product = Product::findOrFail($request->id);
$categories = $product->category;
return $categories;
}
您可以从 ajax 附加类别
jQuery('.orderProduct').click(function(e){
var productId = $(this).attr('id');
var token = '{{ csrf_token() }}';
e.preventDefault();
jQuery.ajax({
url: "{{ url('order/getCategoryAjax') }}",
method: 'post',
data: {
id: productId,
_token: token
},
success: function(data){
$('#orderProductSelect').hide();
$('.categoryAjax').show();
$.each(data, function(index,category){
$('.categoryAjax').append('<div class="col-md-4" style="margin: 10px;"> <div id="'+category.id+'" class="orderProduct" >'+category.name+'</div> </div>');
});
}});
});
在视图文件中
<div class="categoryAjax" style="display: none;">
<div class="row">
<h3>chouse category :</h3>
</div>
</div>
推荐阅读
- ios - LazyVStack 和 SwiftUI 的性能不佳问题
- apache-zookeeper - 如何一次创建多个znode?
- android - 通过 JDBC 使用 Compute Engine 连接到 Cloud SQL
- javascript - Django 项目 - 从引导卡打开模式
- android - Android BLE Gatt 连接问题(连接超时状态:8)
- python - Python数据框计算图和列摘要之间的比率
- java - 如何重置变量但不更改旧值?
- algorithm - 渐近下界不起作用。为什么?
- python-3.x - 如何以数组形式显示与 cv2 连接的多个窗口?
- stm32 - STM32 寄存器值不变