Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

maltekiefer's avatar

API not working with Laravel 8

Good morning,

in my api routes I have this:

Route::post('/setClient', 'App\Http\Controllers\ClientController@setClient');

The method looks like this:

    public function setClient (Request $request){

        $request->validate([
            'pushtoken' => 'required|string',
            'allownotification' => 'required|boolean'
        ]);

        $client = Clients::updateOrCreate(
            ['pushtoken' => $request->pushtoken],
            ['allownotification' => $request->allownotification]
        );

        return response()->json(null, 200);
    }

But when I tried to make a post request with Postman I only see the home page in the response and in the database is nothing. When I change post to get in the route and open it in the browser I was instantly redirected to /.

0 likes
6 replies
Sinnbeck's avatar

What url are you calling?

And do you have a catch all route in web.php?

maltekiefer's avatar

This is my web.php

Route::get('/', function () {
    return view('welcome');
});

Auth::routes([
    'register' => false, // Registration Routes...
    'reset' => false, // Password Reset Routes...
    'verify' => false, // Email Verification Routes...
]);

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

And I call this route:

http://127.0.0.1:8000/api/setClient

Sinnbeck's avatar

Ok try checking if it is the validation. Comment out everything but the response

maltekiefer's avatar

It was the validation. I do this now:

    public function setClient (Request $request){

 /*       $request->validate([
            'pushtoken' => 'required|string',
            'allownotification' => 'required|boolean'
        ]);
**/
        $client = Clients::updateOrCreate(
            ['pushtoken' => $request->pushtoken],
            ['allownotification' => $request->allownotification]
        );

        return response()->json(null, 200);
    }

But then I get this error:

Illuminate\Database\Eloquent\MassAssignmentException: Add [pushtoken] to fillable property to allow mass assignment on [App\Models\Clients]. in file /Users/maltekiefer/Entwicklung/uadblock/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 354

I send with Postman this in the body:

https://i.imgur.com/HJybEJw.png

Sinnbeck's avatar

Good to hear. You might want to work on that validation untill it works as expected. And yes remember to allow mass assignment when using create or update :)

Please or to participate in this conversation.