首页 > 解决方案 > 在刀片 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)

标签: phpajaxlaravellaravel-5laravel-blade

解决方案


您需要在此函数中返回响应,而不是整个视图

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>

推荐阅读