Laravel 6.2 API, DELETE and PUT not working - laravel

Edit: Solved: the problem was the trailing slash on the urls
I cannot use the destroy() and update() methods of my API, it executes only the show() method.
routes:list shows all routes correctly and the controller has all required methods.
Authentication works correctly over Bearer Token.
api.php:
Route::apiResource('subscriptions', 'Api\SubscriptionController')->middleware('auth:api');
called urls:
DELETE http://127.0.0.1/api/subscriptions/2/
DELETE http://127.0.0.1/api/subscriptions/2/?_method=delete
Same with PUT and POST instead of DELETE, always executes the show() method.
store() and index() methods are working.
Used Versions:
Laravel 6.2
Php 7.2.23
Postman 7.13 (for requests)

Related

Redirect Vue application with Laravel

I am trying to redirect the Vue.js application with Laravel's redirect() method but seemingly it doesn't work with Vue.js. Currently I am returning a redirect in one of my controllers in this way:
return redirect(url);
The redirection works when I call the endpoint from Chrome, but it doesn't work when the endpoint is called from Vue.js.
I agree with the last comment You should try another type of return.
for example.
public function YourFunction()
{
return view('url');
}

Laravel 8: 302 Redirection when POST/PUT methods called

I've newly installed Laravel 8, and setup my api resources.
But when I try to create/update a record, it's redirect me with 302to the home page...
Here is my api.php:
Route::apiResource('addresses', AddressController::class);
In my AddressController.php, my store method:
public function store(CreateAddressRequest $request)
{
return response()->json(Address::create($request->validated()));
}
Need help (to understand), please.
Please set the header in postman - Accept: application/json.
When you create through store() method, you have CreateAddressRequest class which validates your input. When the validation fails it redirects to the previous page and it could redirects to the homepage if there is no previous page.
For API, this behavior is not desirable since you want to return error message (in JSON) instead of redirection.
Example of using validator to return the error. REST API in Laravel when validating the request

How to fix 'The GET method is not supported for this route. Supported methods: POST'

I'm developing a REST service with Laravel to consume it from a mobile app. It works properly on local, but not on hosting. After several tries, I developed a basic example to test the POST method, but it returns the same error.
api.php file
Route::post('/test', 'testController#test') ;
testController.php file
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class testController extends Controller
{
//
public function test(Request $request)
{
return response()->json(['mensaje' => 'POST access successful']);
}
}
POST request is always returning the same error, and I am using POST on petition: 405 Method Not Allowed. The GET method is not supported for this route. Supported methods: POST.
POSTMAN request
I have investigated this topic and I have read it could be due CORS. So, I have installed spatie/laravel-cors with its default config, but POSTMAN is still showing the same error. Some help, please?
SOLVED:
Thanks all! Definitely, it was not a CORS problem. My hosting server makes a redirect by default, losing POST parameters in the way.
If you have come across this error while using POSTMAN, you need to go into the "Settings" part of your route's parameters and disable 'Automatically follow redirects'.
Sometimes the problem arises when you have trailing froward slash.
For example:
https://somedomain.com/api/test/
Simply remove the trailing forward slash so the url will be
https://somedomain.com/api/test
It's sometimes because you're using 'http' on an 'https'-based endpoint. Try replacing the http with https.
This is because of CORS (Cross Origin Resource Sharing) is protected and you are not allow to call your api from other origin. To allow put below header setting to your routes in api.php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE');
header('Access-Control-Allow-Headers: Content-Type, X-Auth-Token, Origin, Authorization');
Are you sure you call /api/test because in api.php route file there is a prefix on routes

No notifications are returned

I am trying to get user's unread notifications though my controller.
This works:
public function notifications(){
return \App\User::find(auth()->user()->id)->unreadNotifications()->limit(5)->get();
}
This doesn't, it returns an empty collection:
public function notifications(){
return auth()->user()->unreadNotifications()->limit(5)->get();
}
Could you tell me what I am missing? Thanks in advance.
Using Laravel 5.8 with Backpack 3.5.
The default auth guard of Laravel is overwitten to use Backpack auth in backpack routes, using the UseBackpackAuthGuardInsteadOfDefaultAuthGuard middleware of the permissions manager package. In the rest of the controller auth() and backpack_auth works normally.
Try this:
public function notifications()
return Auth::user()->unreadNotifications()->limit(5)->get();
}
As said in the docs:
You may access the authenticated user via the Auth facade:
Alternatively, once a user is authenticated, you may access the authenticated user via an Illuminate\Http\Request instance. Remember, type-hinted classes will automatically be injected into your controller methods:
Auth and auth() likely don't work here because you're using the Backpack For Laravel authentication which uses a different guard than the default one Laravel uses.
This would probably work for you:
backpack_user()->unreadNotifications()->limit(5)->get();
If that works, here's why:
If you take a look at project/vendor/backpack/base/src/helpers.php you'll see that backpack_user() is an alias for backpack_auth()->user() and backpack_auth does a:
return \Auth::guard(backpack_guard_name());
That's the important bit because it grabs the guard defined config/backpack/base.php (which is backpack by default) and uses that instead of Laravel's default guard of web.

Laravel auth gurad check is not working in constructor

Having the below code in constructor,
public function __construct(){
if (Auth::guard('admin')->check()){
dd(Auth::guard('admin')->user()->name);
}
}
This is not working.
But this is working in other controller functions.
Since Laravel 5.3 you are no longer able to access session (and thus Auth stuff as well) in controller constructors, because session middleware has not run yet.
5.3 changes - scroll to "Session In The Constructor" to see how to get around it.

Resources