我创建了一个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');
}}
现在我在创建时出错了。刀身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>
在刀片服务器的这一部分中,您有变量$products
,但您没有从控制器接收到它
@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
首先,我建议您将您的关系从category
重命名为categories
。这是一对多,对吗?然后,您只能使用$categories=$product>访问关系-
您还可以尝试使用dd($someVariable)
检查$variables中的任何数据。