mardi 25 septembre 2018

can't find passed data with controller in blade laravel 5.6

I created a laravel project . I use laravel 5.6

show products and category in balde with bellow code :

<div id="orderProductSelect" class="row">

       <h2>chouse product :</h2>

        @foreach($products as $product)
                <div class="col-md-4" style="margin: 10px;">
                    <div id="" class="orderProduct" style="cursor: pointer;">
                        <img style="width: 100%;height: 225px;" src="/images/">
                        <span style="display: block;"></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="" class="orderProduct" style="cursor: pointer;">
                            <img style="width: 100%;height: 225px;" src="/images/">
                            <span style="display: block;"></span>
                        </div>
                    </div>
                @endforeach
            </div>
        </div>

and send product ID with Ajax :

jQuery('.orderProduct').click(function(e){
    var productId = $(this).attr('id');
    var token = '';

    e.preventDefault();

    jQuery.ajax({
        url: "",
        method: 'post',
        data: {
            id: productId,
            _token: token
        },
        success: function(data){
            $('#orderProductSelect').hide();
            $('.categoryAjax').show();
        }});
});

I get product Id in order controller . now find product category :

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();
    }

}

Now I have error in create.blade.php page :

Trying to get property of non-object (View: /home/projects/resources/views/user/profile/order/create.blade.php)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire