How to display data returned from ajax response as a view in laravel
<p>I'm new to Ajax, and want to return a partial view in laravel in response of an ajax call, so when I click on a link to display the data in a modal it does not work and saying trying to get property of non object. any help?</p>

<p>This is my code:</p>

<pre><code>&lt;tr&gt;
    &lt;td style="border-right: 1px solid #ddd;"&gt;&lt;?php echo $i;?&gt;&lt;/td&gt;
    &lt;td&gt;{{ $sale-&gt;item_name }}&lt;/td&gt;
    &lt;td&gt;{{ $sale-&gt;brand_name }}&lt;/td&gt;
    &lt;td&gt;{{ $sale-&gt;category_name }}&lt;/td&gt;
    &lt;td&gt;{{ $sale-&gt;fname }} &amp;nbsp; {{ $sale-&gt;lname }}&lt;/td&gt;
    &lt;td&gt;{{ $sale-&gt;sale_date }}&lt;/td&gt;
    &lt;td&gt;{{ $sale-&gt;quantity }}&lt;/td&gt;
    &lt;td&gt;{{ $sale-&gt;unit_name }}&lt;/td&gt;
    &lt;td&gt;&lt;span&gt;$&lt;/span&gt;{{ $sale-&gt;unit_price }} &lt;/td&gt;
    &lt;td&gt;&lt;span&gt;$&lt;/span&gt;&lt;?php echo $sale-&gt;quantity*$sale-&gt;unit_price;?&gt; &lt;/td&gt;
    &lt;td&gt;
        &lt;a href="#bill" class="btn btn-xs btn-green mr-5" type="button" tabindex="0" data-toggle="modal" data-target="#reportModal" onclick="load_modal_data('{{ $sale-&gt;sale_id }}','/sales/bill','billContent')"&gt;
        &lt;i class="fa fa-pencil"&gt; Bill&lt;/i&gt;&lt;/a&gt;
        &lt;a href="/purchase/item" class="btn btn-xs btn-green mr-5"&gt;&lt;i class="fa fa-search"&gt; View&lt;/i&gt;&lt;/a&gt;
        &lt;a href="#" class="btn btn-xs btn-lightred"&gt;&lt;i class="fa fa-remove"&gt; Del&lt;/i&gt;&lt;/a&gt;
    &lt;/td&gt;
&lt;/tr&gt;
</code></pre>

<p><strong>The JS and Ajax</strong></p>

<pre><code>function load_modal_data(identity, route,target_tag)
{
    $.ajax({
        headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') },
        url: route,
        type:'post',
        data:{ id: identity},
        success:function(result){
            console.log(result)
            $('#'+target_tag).html(result);
        }
    })
}
</code></pre>

<p><strong>The Controller:</strong></p>

<pre><code>public function bill()
{
    $id = Input::get('identity');
    $sales = DB::table('sales')
        -&gt;join('brands','brands.bid','=','sales.brand_id')
        -&gt;join('units','units.unit_id','=','sales.unit_id')
        -&gt;join('categories','categories.cat_id', '=','sales.category_id')
        -&gt;join('customers','customers.cid','=','sales.c_id')
        -&gt;select('sales.*','brands.brand_name','categories.category_name','customers.fname','customers.lname','units.unit_name')
        -&gt;where('sales.sale_id',$id)-&gt;first();
    $returnHTML = view('partials.item-bill')-&gt;with('sales',$sales)-&gt;render();
    return response()-&gt;json(array('success'=&gt;true, 'html'=&gt;$returnHTML));
}
</code></pre>

<p>This is my <strong>item-bill.blade.php</strong></p>

<pre><code>&lt;!-- tile body --&gt;
&lt;div class="tile-body p-0"&gt;
    &lt;div class="table-responsive"&gt;
        &lt;table class="table mb-0" id="usersList"&gt;
            &lt;thead&gt;
            &lt;tr&gt;
                &lt;th style="width:20px;"&gt;No&lt;/th&gt;
                &lt;th&gt;Item Name&lt;/th&gt;
                &lt;th&gt;Company&lt;/th&gt;
                &lt;th&gt;Category&lt;/th&gt;
                &lt;th&gt;Customer&lt;/th&gt;
                &lt;th&gt;Sale Date&lt;/th&gt;
                &lt;th&gt;Quantity&lt;/th&gt;
                &lt;th&gt;Unit&lt;/th&gt;
                &lt;th&gt;Price&lt;/th&gt;
                &lt;th&gt;Total&lt;/th&gt;
            &lt;/tr&gt;
            &lt;/thead&gt;
            &lt;tbody&gt;
                &lt;tr&gt;
                    &lt;td&gt;{{ $sales-&gt;item_name }}&lt;/td&gt;
                    &lt;td&gt;{{ $sales-&gt;brand_name }}&lt;/td&gt;
                    &lt;td&gt;{{ $sales-&gt;category_name }}&lt;/td&gt;
                    &lt;td&gt;{{ $sales-&gt;fname }} &amp;nbsp; {{ $sales-&gt;lname }}&lt;/td&gt;
                    &lt;td&gt;{{ $sales-&gt;sale_date }}&lt;/td&gt;
                    &lt;td&gt;{{ $sales-&gt;quantity }}&lt;/td&gt;
                    &lt;td&gt;{{ $sales-&gt;unit_name }}&lt;/td&gt;
                    &lt;td&gt;&lt;span&gt;$&lt;/span&gt;{{ $sales-&gt;unit_price }} &lt;/td&gt;
                    &lt;td&gt;&lt;span&gt;$&lt;/span&gt;&lt;?php echo $sales-&gt;quantity*$sales-&gt;unit_price;?&gt; &lt;/td&gt;
                &lt;/tr&gt;
            &lt;/tbody&gt;
        &lt;/table&gt;
    &lt;/div&gt;
</code></pre>

<p>Note: I'm using Laravel 5.2</p>