Dingo api $response is undefined - laravel

Getting error
"message": "Undefined property: App\Http\Controllers\Api\ApiUserXYzController::$response"
return $this->response->collection($user_list, new ProjectTransformer())->setStatusCode(200);
I"m tring to transform the data but i'm getting $response is not defined. please guide

You forgot to include the helper trait that the docs tell you to use if you want the response builder at the property response on the controller.
Dingo Wiki - Responses - Response Builder
use Dingo\Api\Routing\Helpers;
class SomeController ....
{
use Helpers;
...
}

Related

Laravel: Missing Argument error on a POST

I keep getting the error "Missing argument 1 for App\Http\Controllers\Users::deleteMobileAssets()" . I am making a call from my frontend with Vue. When I check the headers, it seems correct but I'm not sure what is causing the error. I've tried wrapping imageType in brackets too: {imageType: imageType} but still same error.
deleteImage(imageType) {
axios.post('/delete-mobile-assets', imageType);
}
public function deleteMobileAssets($imageType)
{
}
POST data is included in the body of the request that way you are getting Missing argument 1. Try this
deleteImage(imageType) {
axios.post('/delete-mobile-assets', {imageType:imageType});
}
public function deleteMobileAssets(Request $request)
{
$request->imageType
}
Or if you want to implement DELETE method. have a look Delete Method in Axios, Laravel and VueJS

How to authorize user owns resource using API through middleware in Laravel?

I need to check if user is permitted to view resource. Request is sent by API and passes through auth:api middleware. I'm using Laravel 5.8
I tried to use middleware in route declaration like this:
Route::get('/user/{id}', 'UserController#get')->middleware('can:view,user')
or in method's controller code like this:
$user = Auth::guard('api')->user();
$this->authorize('view', $user, $anotherUserModel);
Each of these methods either lets user pass policy even it's hardcoded to stops user form achieving this or throws error:
{
"message": "This action is unauthorized.",
"exception": "Symfony\\Component\\HttpKernel\\Exception\\AccessDeniedHttpException",
"file": "C:\\xampp\\htdocs\\laravelapitest\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Exceptions\\Handler.php",
"line": 202,
[...]
}
but I'm expecting that user should be stopped and response code should be 403
Can you please give this a try and let me know if it fixes your problem.
Go to App\Exceptions folder and add the following at the top of the Handler.php file:
use Illuminate\Auth\Access\AuthorizationException;
add the following to $dontReport array in the same file:
\Illuminate\Auth\Access\AuthorizationException::class,
and the last step is to customize error response by adding the following code in the render function:
if ($exception instanceof AuthorizationException) {
return response()->json('unauthorized',403);
}

Laravel 5.7 ApiResource GET params empty

I use Laravel 5.7 for my JSON API web application.
In my routes/api.php file, I created the following route :
Route::apiResource('my_resource', 'API\Resource')->except(['delete']);
I added the corresponding controller and methods (index, show,...) and everythink work perfectly. My issue is the following : I would like to add optional GET params like this :
http://a.x.y.z/my_resource?param=hello&param2=...
And for instance being able to retrieve 'hello' in my index() method. However, when I print the value of $request->input('param'), it's empty. I just don't get anything.
Yet, if I create a route like this, with an optional parameter:
Route::get('/my_resource/{param?}', 'API\Resource');
I'm able to get the parameter value in my controller method.
Here is my index method :
class Resource extends Controller {
public function index(Request $request)
{
print($request->input('param'));
// ...
}
// ...
}
Am I missing something ? I'm still new in Laravel maybe I missed something in the documentation.
Thanking you in advance,
You can use:
$request->route("param");

Laravel: trying to make three update functions within one controller

I'm trying to make three different update functions in CompanyAdressController: defaultUpdate, contactUpdate and generalUpdate.
In first case I'm trying to access them via api:
from my js app:
this.$http.put('http://127.0.0.1:8000/api/companyDefault/' + this.hospital.default.id, this.hospital.default)
and inside api routes:
Route::resource('/companyDefault', 'CompanyAddressController#defaultUpdate');
and in CompanyAddressController:
public function defaultUpdate(Request $request, CompanyAddress $companyAddress)
{
...
}
I've got an error:
"message": "Method [defaultUpdate#update] does not exist on [App\\Http\\Controllers\\CompanyAddressController].",
"exception": "BadMethodCallException",
How should I correct my routes to get access to my method? Or should I do it different way by making one controller update function with parameters from my api function?
You're using resourceful controller check laravel documentation: https://laravel.com/docs/5.6/controllers#resource-controllers. The proper syntax for registering a resourceful controller is:
Route::resource('companyDefault', 'CompanyAddressController');
I think this is what you want:
Route::put('/companyDefault', 'CompanyAddressController#defaultUpdate');
Route::put('/contactUpdate', 'CompanyAddressController#contactUpdate');
Route::put('/generalUpdate', 'CompanyAddressController#generalUpdate');

Laravel save() not working on new object

I'm trying to use the code provided by the documentation of entrust in a controller but save() method wont execute and gives me the error
Method [save] does not exist on [App\Http\Controllers\role]
Here is the code:
$cityadmin = new Role();
$cityadmin->name= 'cityadmin';
$cityadmin->save();
Use full namespace:
$cityadmin = new \App\Role;
Or add this to the top of the controller:
use App\Role;

Resources