dimanche 29 mai 2016

How to display data returned from ajax response as a view in laravel

everyone 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 none object. any help. this is my code:

<tr>
        <td style="border-right: 1px solid #ddd;"><?php echo $i;?></td>
         <td></td>
         <td></td>
         <td></td>
        <td> &nbsp; </td>
        <td></td>
         <td></td>
        <td></td>
        <td><span>$</span> </td>
        <td><span>$</span><?php echo $sale->quantity*$sale->unit_price;?> </td>
    <td>
       <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('','/sales/bill','billContent')">
                                            <i class="fa fa-pencil"> Bill</i></a>
                                        <a href="/purchase/item" class="btn btn-xs btn-green mr-5"><i class="fa fa-search"> View</i></a>
                                        <a href="#" class="btn btn-xs btn-lightred"><i class="fa fa-remove"> Del</i></a>
                                    </td>
                                </tr>

the Js and Ajax:

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

the controller:

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

Note: I'm using Laravel 5.2



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire