1

I'm trying to send 2 variables to FavorisController: {{ Auth::user()->id }} 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 = "{{ Auth::user()->id }}" ;
        $.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 {{ Auth::user()->id }}

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.

4
  • 1
    Is the user logged in? If not, there is no id. Also, you have the URL incorrect in your ajax. You need to append id_user to the URL instead of using {id} Commented Apr 10, 2017 at 15:20
  • You will also need to pass the CSRF token. Docs <script> window.Laravel = {!! json_encode([ 'csrfToken' => csrf_token(), ]) !!}; </script> Commented Apr 10, 2017 at 15:20
  • Oh you're right now I can acces to the page. But it don't execute the ajax code I tried an alert but nothing shows up Commented Apr 10, 2017 at 15:33
  • But It's not the id_user I want in the url it's the id_annonce Commented Apr 10, 2017 at 15:37

2 Answers 2

1

Move Auth::user() part from JS script to PHP:

->insertGetId(['annonce_residentiel_id' => $id_annonce , 'user_id' => auth()->user()->id]);

Also, make sure user is authenticated with something like:

if (auth()->check()) {
    // Do stuff.
}
Sign up to request clarification or add additional context in comments.

1 Comment

That's correct I guess I have the error in my ajax code : data attribute
0

Based on your route definition $id is the argument for the create method and to get more data from the ajax call you should use Input::get. Also the route will be activated only when the url in the ajax call has the format of ex: annonce_test/25. check the update to the ajax call.

Route:

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

Ajax call:

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

FavorisController:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.