【问题标题】:Laravel POST route returns HTTP code 200 but no record is createdLaravel POST 路由返回 HTTP 代码 200 但没有创建记录
【发布时间】:2021-06-27 09:59:56
【问题描述】:

我创建了一个 Laravel API 端点 api/post/offer,它将在其中创建一个帖子。 使用邮递员的邮寄请求。 postman post request

然后会访问api.php处的路由

Route::post('post/offer', [PostController::class, 'create_offer_post'])->name('create_offer_post');

在我的 PostController 中使用 create_offer_post 方法

public function create_offer_post(Request $request) {

        //validate data
        $request->validate([
            'email' => ['required', 'email', 'max:50'],
            'postIdentity' => ['required', 'max:100'],
            'postStatus' => ['required', 'max:50'],
            'deliveryArea' => ['required', 'max:500'],
            'shoppingPlace' => ['required', 'max:2000'],
            'deliverySchedule' => ['required', 'date'],
            'transportMode' => ['required', 'max:200'],
            'capacity' => ['required', 'max:100'],
            'paymentMethod' => ['required', 'max:200'],
            'caption' => ['required', 'max:200'],
            'isLoggedIn' => ['required', 'boolean']
        ]);

        dd($request->all());

        // if logged in create offer post
        if($request->isLoggedIn) {
            DB::transaction(function () {

                $post = new Post;
                $post->email = $request->email;
                $post->postIdentity = $request->postIdentity;
                $post->postStatus = $request->postStatus;
                $post->save();

                $offer_post = new OfferPost;
                $offer_post->postStatus = $request->postStatus;
                $offer_post->deliveryArea = $request->deliveryArea;
                $offer_post->shoppingPlace = $request->shoppingPlace;
                $offer_post->deliverySchedule = $request->deliverySchedule;
                $offer_post->transportMode = $request->transportMode;
                $offer_post->capacity = $request->capacity;
                $offer_post->paymentMethod = $request->paymentMethod;
                $offer_post->caption = $offer_post->caption;
                $post->offer_post()->save($offer_post);
            });

            return response()->json(['message' => 'Offer post successfully created.'], 201);
        }
        else {

            return response->json(['error' => 'You are not logged in.'], 401);
        }
    }

它验证请求。然后它应该将帖子保存在数据库中。 我的问题是每当我在邮递员中发送请求时。它发送一个 200 响应代码。我期待 201 或 401 响应代码。但我收到 200 响应代码,并且数据库中没有保存新记录。 dd() 也不工作

【问题讨论】:

标签: php laravel rest routes


【解决方案1】:

您已经启动了数据库事务,但您没有提交它以将数据保存在数据库中。也许是这个错误背后的原因。

【讨论】:

    【解决方案2】:

    看起来我的验证码不起作用并返回 200。同时我删除了验证。

    【讨论】:

      【解决方案3】:

      尝试在邮递员中使用原始数据而不是表单数据。

      【讨论】:

      • 是我的请求格式错误。我还需要设置 Accept header = application/json.
      【解决方案4】:

      请检查表单数据中是否缺少参数[必需]。这就是为什么没有在您的数据库中创建数据的原因

      【讨论】:

        【解决方案5】:
        dd($request->all());
        

        此行转储数据并退出。您的代码的下一行将不会运行。

        【讨论】:

          猜你喜欢
          • 2018-11-23
          • 2012-08-14
          • 2014-01-24
          • 1970-01-01
          • 2020-04-28
          • 2018-08-05
          • 1970-01-01
          • 1970-01-01
          • 2019-02-18
          相关资源
          最近更新 更多