php - Ajax request in Laravel returns an empty object -


i'm using laravel & jquery in order insert data mysql table , take new id of new row. problem response empty object. have tried print_r result in controller got huge array.

here code.

jquery :

        $.ajax({             url: "locations/new",             type: "get", //send through method             data:{name:name,description:description},             success: function(response) {               $data = $(response);               console.log($data);                              },             error: function() {               console.log("unable add")             }         });  

controller :

$name= input::has('name') ? input::get('name') : null;         $description= input::has('description') ? input::get('description') : null;          $add = location::add($name, $description);          return  $add; 

and model:

    public static function add($name,$description){         $location_id = db::table('locations')->insertgetid(         array('name' => $name, 'description' => $description)         );         self::get($location_id);           }      public static function get($location_id){         $result = db::table('locations')->where('location_id',$location_id)                   ->select(db::raw('location_id,name'));          $result->get();          return $result;      } 

i know might have mistake here. please except answers know reason of mistake.

thanks in advance..

after co-investigating (@chat), problem seemed in query builder.

after adding var_dump($result) output was:

error: syntax error, unrecognized expression: object(illuminate\database\query\builder)#248 (27) { ["connection":prot 

i suggested removing db::raw function can pass fields select function, , convenient reasons attach ->get() function same line. final query builder code be:

$result = db::table('locations')->where('location_id',$location_id)->select('location_id',‌​'name')->get(); 

which solved problem.

update - reason

after looking deeper in source of framework, i've found out that:

/**  * set columns selected.  *  * @param  array  $columns  * @return $this  */ public function select($columns = array('*')) {     $this->columns = is_array($columns) ? $columns : func_get_args();     return $this; } 

so select function expecting array or arguments columns, , when db::raw in use - returned string conflicted expected parameters of select function.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -