I
I
Ilya Loopashko2021-07-07 07:30:48
PHP
Ilya Loopashko, 2021-07-07 07:30:48

How to check HTTP status?

There is an implementation of the method for creating an entry in the database, I need to check it. How to implement it, I can not figure it out yet. I need to execute the else block in case the entry is not created.

public function store(Request $request)
    {
        $idle = new Idle;
        $idle->fill($request->idle)->save();
        if (201) [$status = "OK", $message = "Запись создана"];
        else [$status = "Error", $message = "Запись не создана"];
        return response()->json([
            "status" => $status,
            "message" => $message,
        ], 201);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pLavrenov, 2021-07-07
@deadloop

1) Incorrect PHP constructs
2) 201 is the server response code for the request, not that "record was not created" HTTP 3) Laravel throws an exceptions exceptions Laravel report-helper
if the record was not created for any reason

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question