I am leaning laravel. I am working on Rest API
. I have made the controller
to post using curl as follow
public function post_index()
{
$cat = Input::json();
$dbCat = new Category();
$dbCat->code = $cat->code;
$dbCat->name = $cat->name;
$dbCat->save();
return $dbCat->toJson();
}
when I am trying to post using curl then i am getting following error
Message:
Trying to get property of non-object
Location:
C:\xampp\htdocs\NewBlog\application\controllers\api\category.php on line 22
and the stack trace is
#0 C:\xampp\htdocs\NewBlog\laravel\laravel.php(42): Laravel\Error::native(8, 'Trying to get p...', 'C:\xampp\htdocs...', 22)
#1 C:\xampp\htdocs\NewBlog\application\controllers\api\category.php(22): Laravel\{closure}(8, 'Trying to get p...', 'C:\xampp\htdocs...', 22, Array)
#2 [internal function]: Api_Category_Controller->post_index()
#3 C:\xampp\htdocs\NewBlog\laravelouting\controller.php(325): call_user_func_array(Array, Array)
#4 C:\xampp\htdocs\NewBlog\laravelouting\controller.php(285): Laravel\Routing\Controller->response('index', Array)
#5 C:\xampp\htdocs\NewBlog\laravelouting\controller.php(165): Laravel\Routing\Controller->execute('index', Array)
#6 C:\xampp\htdocs\NewBlog\laraveloutingoute.php(153): Laravel\Routing\Controller::call('api.category@in...', Array)
#7 C:\xampp\htdocs\NewBlog\laraveloutingoute.php(124): Laravel\Routing\Route->response()
#8 C:\xampp\htdocs\NewBlog\laravel\laravel.php(167): Laravel\Routing\Route->call()
#9 C:\xampp\htdocs\NewBlog\public\index.php(34): require('C:\xampp\htdocs...')
#10 {main}
how can I come out through this problem ?
Without knowing what line 22 is, my suggestion would be to check that $cat
is actually an object. It's most likely throwing that error because it's null
or, as the error says, a non-object.
Since you are filling $cat with Input::json() you'll need to make sure that you are sending a curl request that has a header that is setting the content type to application/json
curl isn't really built for sending json, are you able to send normal post fields instead?
Really... we need more information on your situation, like the code you are using to send a curl request
If it is working as indicated in the other answer, and you are now encountering a problem with a "doubling up" of the json wrapper then I would suggest using eloquent_to_json() instead of json_encode() http://laravel.com/api/function-eloquent_to_json.html