Tuesday, April 11, 2017

Laravel Pass Data to controller through ajax

I'm trying to send 2 variables to FavorisController: and the id that I have in the URL

This is what I've tried so far

Route:

Route::get('annonce_test/{id}','FavorisController@create' );

My ajax script

$(document).ready(function() {
    $('.favoris').click(function(){                  
        var id_user = "" ;
        $.ajax({
            url: 'annonce_test/{id}',
            type: 'GET',             
            data: {id: 1, id_user: $id_user},
            success: function(data){
                alert(Ajouter au Favoris avec succes);
            },
            error: function(){},
        });""
    });         
});

FavorisController

public function create(Requests $request)
{
    $id_annonce = $_GET['id'];
    $id_user = $_GET['id_user'];
    $query = DB::table('annonce_residentiel_user')
    ->insertGetId(
        array('annonce_residentiel_id' => $id_annonce , 'user_id' => $id_user)
        );
}

I got the error trying to get property of non-object

But is this the correct way to do it? I mean if I have another script for deleting I should chage the url in my ajax script.



via Marouane Sihad

Advertisement