我创建了一个laravel项目。我使用laravel 5.6
用下面的代码在秃头中显示产品和类别(正确接收产品。):
<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)
答案 0 :(得分:2)
您需要在此函数中返回响应,而不是整个视图
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>
答案 1 :(得分:0)
在Blade的此部分中,您有变量$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
答案 2 :(得分:0)
首先,我建议您将您的关系从category
重命名为categories
。这是一对多的,对吗?
然后,您只能使用$categories = $product->categories
而不使用get()
来访问关系。
此外,您可以尝试使用dd($someVariable)
检查$变量中的所有数据。