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

can definition
can synonym
can.'' acronym
can band
meaning of can in hindi
can y
cant
can pronunciation

I created a laravel project . I use laravel 5.6

show products and category in balde with bellow code (Products are received correctly.):

<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>

and send product ID with 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();
        }});
});

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

}

my Product Model :

class Product extends Model{

protected $fillable = [ 'name', 'filename', 'description'];

public function category(){

    return $this->hasMany(Category::class,'product_id');
}}

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

Undefined variable: categories (View: /home/laravel-projects/resources/views/user/profile/order/create.blade.php)

You need to return a response in this function, not entire view

public function getCategoryAjax(Request $request){

    $product = Product::findOrFail($request->id);

    $categories  = $product->category;

    return $categories;

}

You can append category from 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>');
                        });
                    }});
            });

In view file

<div class="categoryAjax" style="display: none;">
            <div class="row">

                <h3>chouse category :</h3>

            </div>
        </div>

Can, Can was the leading avant-garde rock group of the 70s. Can experimented with noise, synthesizers, non-traditional music, cut-and-paste techniques, and, most  Can definition, to be able to; have the ability, power, or skill to: She can solve the problem easily, I'm sure. See more.

In this part of your Blade you have variable $products, but you not receive it from controller

@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

Can, VerbEdit. can (third-person singular simple present can, present participle -, simple past could, past participle (obsolete except in  Define can. can synonyms, can pronunciation, can translation, English dictionary definition of can. to be able to, have the power or skill to: I can take a bus to the

First i advise you to rename your relationship from category to categories. It is one to many, right? Then you can access to relation only using $categories = $product->categories without get().

Also you can try to use dd($someVariable) to check any data in your $variables.

The Official CAN / Spoon Records Website, can meaning: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more. can definition: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more.

can, 1.1Be able to through acquired knowledge or skill. 'I can speak Italian'. More example sentences. 110 synonyms of can from the Merriam-Webster Thesaurus, plus 97 related words, definitions, and antonyms. Find another word for can. Can: to bring (as an action or operation) to an immediate end.

CAN, Explore Our Can-Am ATV & Side by Side Lineup. Person sitting on a Ryker · On-​Road. Explore Our Can-Am Three-Wheel Motorcycle Lineup. © BRP 2003-2020. Create beautiful designs with your team. Use Canva's drag-and-drop feature and layouts to design, share and print business cards, logos, presentations and more.

Can, VEHICLE LINEUP. CAN-AM VS THE REST. The world is calling. From the trails to fields, dunes to mountaintops, this beautiful, challenging  Create beautiful designs with your team. Use Canva's drag-and-drop feature and layouts to design, share and print business cards, logos, presentations and more.

Comments
  • on which line number, are you getting an error ?
  • Thanks . edited question .
  • do you have defined category relation with product ?
  • You have two divs with class="orderProduct" but with different id. I guess clicking on one of them won't find any $product and thus no $categories. Also if ($request->ajax()) { should return a JSON response, not a rendered view.
  • @Pratik . yes . question edited . add Product Model
  • Thanks . edited question . I pass product with create function controller . Products are received correctly.
  • The error says categories though: Undefined variable: categories
  • Got it. Can you show your Product model in question?
  • Are blade code in your questions is user.profile.order.create?