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

zaster's avatar

Api - Store

This works

class CategoryController extends Controller
{
    public function store(Request $request)
    {
        return Category::create(['name' => $request->name]);
    }
}

This doesn't work

The Api client gives an error

"message": "This action is unauthorized.",

class CategoryController extends Controller
{
    public function store(StoreCategoryRequest $request)
    {
        return Category::create(['name' => $request->name]);
    }
}
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreCategoryRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return false;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        return [
            // 'name' => 'required',
        ];
    }
}
0 likes
2 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Show StoreCategoryRequest. Most likely the authorize() method returns false

zaster's avatar

@sinnbeck

I was formatting the question and your answer was already there.

Yes. Made it true and it works now.

Thank you @sinnbeck

Please or to participate in this conversation.