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\laravel\routing\controller.php(325): call_user_func_array(Array, Array)
#4 C:\xampp\htdocs\NewBlog\laravel\routing\controller.php(285): Laravel\Routing\Controller->response('index', Array)
#5 C:\xampp\htdocs\NewBlog\laravel\routing\controller.php(165): Laravel\Routing\Controller->execute('index', Array)
#6 C:\xampp\htdocs\NewBlog\laravel\routing\route.php(153): Laravel\Routing\Controller::call('api.category#in...', Array)
#7 C:\xampp\htdocs\NewBlog\laravel\routing\route.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 ?
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
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.
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
Related
I have this strange issue.
It was not happening before and after I pulled form github ,
when I send json responses and send and eloquent object with it, it gives me this error. Presumably when the model gets serialized.
I've tried to parse it with json_encode but still the same issue.
I don't know whats going on , is it the issue of the json_encode or a problem with the eloquent model.It's related to all the models because all of them return the same error when i try to parse them to json.
It was ok before but now I don't know what went wrong.
I've compared commits to see the differences but still no luck.
I could not find anything that might have caused this issue so I'm guessing it is the fault of an nontracked file so I reinstalled the dependencies again through composer install still no luck.
When I reverse to the last commit it works fine so the theory of the untracked files is rubish.
Has anyone have any idea ?
I'm going nuts over this.
This is the stacktrace form when it starts to convert to json :
[2018-04-18 14:22:53] local.ERROR: Call to undefined method Illuminate\Database\Query\Builder::getEntityAttribute() {"exception":"[object] (BadMethodCallException(code: 0): Call to undefined method Illuminate\\Database\\Query\\Builder::getEntityAttribute() at /home/marlind/PhpstormProjects/ital-restaurant/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2483)
[stacktrace]
#0 /home/marlind/PhpstormProjects/ital-restaurant/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(1288): Illuminate\\Database\\Query\\Builder->__call('getEntityAttrib...', Array)
#1 /home/marlind/PhpstormProjects/ital-restaurant/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1470): Illuminate\\Database\\Eloquent\\Builder->__call('getEntityAttrib...', Array)
#2 /home/marlind/PhpstormProjects/ital-restaurant/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(438): Illuminate\\Database\\Eloquent\\Model->__call('getEntityAttrib...', Array)
#3 /home/marlind/PhpstormProjects/ital-restaurant/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(450): Illuminate\\Database\\Eloquent\\Model->mutateAttribute('entity', NULL)
#4 /home/marlind/PhpstormProjects/ital-restaurant/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(109): Illuminate\\Database\\Eloquent\\Model->mutateAttributeForArray('entity', NULL)
#5 /home/marlind/PhpstormProjects/ital-restaurant/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(970): Illuminate\\Database\\Eloquent\\Model->attributesToArray()
#6 /home/marlind/PhpstormProjects/ital-restaurant/vendor/laravel/framework/src/Illuminate/Support/Collection.php(1611): Illuminate\\Database\\Eloquent\\Model->toArray()
#7 [internal function]: Illuminate\\Support\\Collection->Illuminate\\Support\\{closure}(Object(App\\AdminActivity))
#8 /home/marlind/PhpstormProjects/ital-restaurant/vendor/laravel/framework/src/Illuminate/Support/Collection.php(1612): array_map(Object(Closure), Array)
#9 /home/marlind/PhpstormProjects/ital-restaurant/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(237): Illuminate\\Support\\Collection->toArray()
#10 /home/marlind/PhpstormProjects/ital-restaurant/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(970): Illuminate\\Database\\Eloquent\\Model->relationsToArray()
#11 /home/marlind/PhpstormProjects/ital-restaurant/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(237): Illuminate\\Database\\Eloquent\\Model->toArray()
#12 /home/marlind/PhpstormProjects/ital-restaurant/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(970): Illuminate\\Database\\Eloquent\\Model->relationsToArray()
#13 /home/marlind/PhpstormProjects/ital-restaurant/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(999): Illuminate\\Database\\Eloquent\\Model->toArray()
#14 [internal function]: Illuminate\\Database\\Eloquent\\Model->jsonSerialize()
#15 /home/marlind/PhpstormProjects/ital-restaurant/vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php(71): json_encode(Array, 0)
#16 /home/marlind/PhpstormProjects/ital-restaurant/vendor/symfony/http-foundation/JsonResponse.php(50): Illuminate\\Http\\JsonResponse->setData(Array)
#17 /home/marlind/PhpstormProjects/ital-restaurant/vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php(31): Symfony\\Component\\HttpFoundation\\JsonResponse->__construct(Array, 200, Array)
#18 /home/marlind/PhpstormProjects/ital-restaurant/vendor/laravel/framework/src/Illuminate/Routing/ResponseFactory.php(83): Illuminate\\Http\\JsonResponse->__construct(Array, 200, Array, 0)
#19 /home/marlind/PhpstormProjects/ital-restaurant/app/Http/Controllers/BaseController.php(19): Illuminate\\Routing\\ResponseFactory->json(Array)
#20 /home/marlind/PhpstormProjects/ital-
I assume you have a problem with getting custom attribute. You can do that in a model by writing get{name}Attribute. You didn't provide us with any code so it's hard to point an error.
Old question but found it when searching for a problem I encountered.
Given that the stack trace is similar to mine I think the information and solution I found could shed more light on the matter.
Here's a link to Laravel's documentation about mutators as array:
https://laravel.com/docs/9.x/eloquent-mutators#array-object-and-collection-casting
For me the problem was when doing ->toArray() on a model that had attribute mutators.
Solved it by adding the following to the model:
protected $casts = [
'entity' => AsArrayObject::class,
];
I am creating an API for get user by YII2 user api by user id. If I pass user id then it is working good but If I pass string then I get below error.
<pre>An Error occurred while handling another error:
yii\base\InvalidParamException: Response content must not be an array. in /var/www/html/vendor/yiisoft/yii2/web/Response.php:1020
Stack trace:
#0 /var/www/html/vendor/yiisoft/yii2/web/Response.php(313): yii\web\Response->prepare()
#1 /var/www/html/vendor/yiisoft/yii2/web/ErrorHandler.php(128): yii\web\Response->send()
#2 /var/www/html/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\web\NotFoundHttpException))
#3 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\NotFoundHttpException))
#4 {main}
Previous exception:
yii\base\InvalidRouteException: Unable to resolve the request: categories/wewqewqe in /var/www/html/vendor/yiisoft/yii2/base/Controller.php:127
Stack trace:
#0 /var/www/html/vendor/yiisoft/yii2/base/Module.php(523): yii\base\Controller->runAction('wewqewqe', Array)
#1 /var/www/html/vendor/yiisoft/yii2/web/Application.php(102): yii\base\Module->runAction('categories/wewq...', Array)
#2 /var/www/html/vendor/yiisoft/yii2/base/Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
#3 /var/www/html/index.php(16): yii\base\Application->run()
#4 {main}
Next yii\web\NotFoundHttpException: Page not found. in /var/www/html/vendor/yiisoft/yii2/web/Application.php:114
Stack trace:
#0 /var/www/html/vendor/yiisoft/yii2/base/Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
#1 /var/www/html/index.php(16): yii\base\Application->run()
#2 {main}</pre>
I am using swagger for executing APIs and Endpoint of the API is
users/{id}
Work fine when enter
/users/1
Get error when
/users/asdf
Web.php
'users/<id:\d+>' => 'users/',
I have recently faced same issue and it has been solved by the change in routing in web.php in config folder.
Change your url
users/<id> instead of users/<id:\d+>.
I am sorry if this request is in the wrong area.
I am learning to use PHP and the CPanel API. I've got the following code and it's giving me a stack error because I am missing something seemingly simple
$domain = array('username' => 'bobbie', 'domain' => 'bobbie.com', 'pass' => 'bobbie123');
$acct = $cp->whm_api('createacct', $domain);
echo "WHM Create: {$acct->createacct}\n";
I know I'm connecting to WHM properly because my code before this outputs the version of WHM correctly. The above code is giving me an error stating that createacct needs to be passed an array as the first parameter:
WHM Version: 11.54.0.21
PHP Fatal error: Uncaught exception 'Exception' with message 'createacct requires that first parameter passed to it is an array' in /root/whmrm/Cpanel/Service/XmlapiClientClass.php:146
Stack trace:
#0 [internal function]: Cpanel_Service_XmlapiClientClass->createacct('bobbie', 'bobbie.com', 'bobbie123')
#1 /root/whmrm/Cpanel/Service/WHM.php(195): call_user_func_array(Array, Array)
#2 [internal function]: Cpanel_Service_WHM->__call('createacct', Array)
#3 [internal function]: Cpanel_Service_WHM->createacct('bobbie', 'bobbie.com', 'bobbie123')
#4 /root/whmrm/Cpanel/PublicAPI.php(525): call_user_func_array(Array, Array)
#5 /root/whmrm/create_sites_on_server.php(68): Cpanel_PublicAPI->__call('whm_api', Array)
#6 /root/whmrm/create_sites_on_server.php(68): Cpanel_PublicAPI->whm_api('createacct', Array)
#7 {main}
thrown in /root/whmrm/Cpanel/Service/XmlapiClientClass.php on line 146
Line 3 of the output is showing that I'm not sending the data properly. Any help would be appreciated. I've googled and most of the results give me information about the xml_api and how to use that. Thanks for your assistance.
Looking at the source for the PublicAPI class, it appears that if an array is given as the parameters, it only calls the resulting function passing the first element from the array (source).
I was able to create an account using this code:
$cp = Cpanel_PublicAPI::getInstance($config);
$whm = Cpanel_PublicAPI::factory('whm');
$domain = array(
'domain' => 'mydomain.com',
'username' => 'drewt2',
'password' => 'myp4ssw0rd!'
);
$response = $whm->createacct($domain);
You can see the functions and there parameters here: Cpanel_Service_XmlapiClientClass.
Unfortunately, the code hasn't been updated in 5 years, and the examples aren't that helpful so you'll likely have to look through the code to figure out most of what you'll want to do.
When I've gone through the installation steps for Anchor CMS, I'm getting an error (after final step):
Uncaught Exception
Undefined variable: vars
Origin
install/routes.php on line 183
Trace
#0 /home/.../anchor-cms/install/routes.php(183): System\error::native(8, 'Undefined varia...', '/home/...', 183, Array)
#1 [internal function]: {closure}()
#2 /home/.../anchor-cms/system/route.php(165): call_user_func_array(Object(Closure), Array)
#3 /home/.../anchor-cms/system/router.php(158): System\route->run()
#4 /home/.../anchor-cms/system/start.php(46): System\router->dispatch()
#5 /home/.../anchor-cms/install/index.php(33): require('/home/...')
#6 {main}
Any suggestions? I'm running Linux 64bit (elementaryOS).
You may have already resolved this, but the way I got around this was to remove the undefined variable from line 183 of the install/routes.php file completely.
return Layout::create('account' $vars);
So it then looked like this:
return Layout::create('account');
Refreshing the page will then allow you to create an account and login to the admin interface etc.
I am newbie with zf2 framework. For example, now I get this error:
Fatal error: Uncaught exception 'Zend\ModuleManager\Exception\RuntimeException' with message 'Module (ZfcUserOverride) could not be initialized.' in F:\Server\domains\zf2-skeleton\vendor\zendframework\zendframework\library\Zend\ModuleManager\ModuleManager.php:189 Stack trace:
#0 F:\Server\domains\zf2-skeleton\vendor\zendframework\zendframework\library\Zend\ModuleManager\ModuleManager.php(163): Zend\ModuleManager\ModuleManager->loadModuleByName(Object(Zend\ModuleManager\ModuleEvent))
#1 F:\Server\domains\zf2-skeleton\vendor\zendframework\zendframework\library\Zend\ModuleManager\ModuleManager.php(90): Zend\ModuleManager\ModuleManager->loadModule('ZfcUserOverride')
#2 [internal function]: Zend\ModuleManager\ModuleManager->onLoadModules(Object(Zend\ModuleManager\ModuleEvent))
#3 F:\Server\domains\zf2-skeleton\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\ModuleManager\ModuleEvent))
#4 F:\Server\domains\zf2-skeleton\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('loadModules', Object(Zend\ModuleManager\ModuleEvent), NULL)
#5 F:\Server\domains\zf2-skeleton\vendor\zendframework\zendframework\library\Zend\ModuleManager\ModuleManager.php(109): Zend\EventManager\EventManager->trigger('loadModules', Object(Zend\ModuleManager\ModuleManager), Object(Zend\ModuleManager\ModuleEvent))
#6 F:\Server\domains\zf2-skeleton\vendor\zendframework\zendframework\library\Zend\Mvc\Application.php(252): Zend\ModuleManager\ModuleManager->loadModules()
#7 F:\Server\domains\zf2-skeleton\public\index.php(17): Zend\Mvc\Application::init(Array)
#8 {main} thrown in F:\Server\domains\zf2-skeleton\vendor\zendframework\zendframework\library\Zend\ModuleManager\ModuleManager.php on line 189
I formatted it manually, so you can read correct error.
It is hard to do it all time I get some error. How I can make zf2 to output such errors correctly?
I need something like this:
The ZendSkeletonApplication comes with a template that reports errors in a readable format. If you install the ZendSkeletonApplication and build your application off of it, you’ll benefit from this template and other conveniences. Alternatively, you can use just the standard error template or create your own.