vendredi 18 mai 2018

Implementing Jquery ajax get request in laravel

Am more of an angular person so i do not really do well with jquery. I have a project i took over from someone and i have to implement a get request to my controller and append returned data to view. I know this is vague but i would appreciate any help.

<tbody>
        @foreach($restaurant_meals as $meal)
            <tr>
                <td>
                    
                </td>
                <td>
                    $
                </td>

                <td>
                    <a href=""><i class="fa fa-plus-circle"></i></a>
                </td>
            </tr>
        @endforeach
    </tbody>

This is my view. This is my ajax request.

<script type="text/javascript">
$(documnent).ready(function(){

    $.ajax({
        method: 'GET', // Type of response and matches what we said in the route
        url: '/addtocartid' + id, // This is the url we gave in the route
        data: {'id' : id}, // a JSON object to send back
        success: function(response){ // What to do if we succeed
            console.log(response); 
        }
    });
});

Then my Controller would be

public function getaddtoCart(Request $request, $id)
{
    $product = Product::find($id);
    $oldCart = Session::has('cart') ? Session::get('cart')  : null;
    $cart = new Cart($oldCart);
    $cart->add($product, $product->id);
    $request->session()->put('cart', $cart);
    return redirect()->route('home');
}

Any help would be appreciated



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2rUcb5C
via IFTTT

Aucun commentaire:

Enregistrer un commentaire