#Column set to "Not null", but when trying to use PATCH request on API it fails.

7 messages · Page 1 of 1 (latest)

vagrant stream
#

Hello,

so I'm currently dealing with a problem regarding my API. As I said when I'm trying to send PATCH request it fails, and the blame is on my column named class_type_id.
So I tried to dd the request it's getting. I found this.

array:2 [▼ // app\Http\Controllers\Api\V1\SiClassController.php:60
  "letter" => "a"
  "class_type_id" => null
]

Error is here:

{
    "message": "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'class_type_id'     cannot be null (Connection: mysql, SQL: update `si_classes` set `letter` = tpg, `class_type_id` = ? where `id` = 9b0b862a-1378-41ea-9ac2-02cf5795aef7)",
    "exception": "Illuminate\\Database\\QueryException",
//...
}

My request looks like this:

PATCH /api/v1/classes/9b0b862a-1378-41ea-9ac2-02cf5795aef7 HTTP/1.1
Cookie: XSRF-...
Content-Type: application/json
Accept: application/json
Host: 127.0.0.1:8000
Content-Length: 20

{
    "letter": "a"
}
#

I don't understand why doesn't it just use the current class_type_id and instead it provides with value of null...

inner hedge
#

can you show the function, to see how are u updating it ?

vagrant stream
#

sure

vagrant stream
# inner hedge can you show the function, to see how are u updating it ?
public function rules(): array
    {
        $method = $this->method();

        if ($method == self::METHOD_PUT) {
            return [
                'classType' => 'required|exists:App\Models\SiClassType,id',
                'letter' => 'required|max:3|alpha:ascii',
                'start' => 'required|numeric|gt:0'
            ];
        } else {
            return [
                'classType' => 'sometimes|required|exists:App\Models\SiClassType,id',
                'letter' => 'sometimes|required|max:3|alpha:ascii',
                'start' => 'sometimes|required|numeric|gt:0'
            ];
        }
    }

    protected function prepareForValidation(): void
    {
        $this->merge([
            'class_type_id' => $this->classType
        ]);
    }

I suspect that it may be related to the exists rule but not sure...

#

PUT request works just fine...

#
public function update(UpdateSiClassRequest $request, SiClass $class)
    {
        $class->update($request->all());
    }