laravel 8 中 API 未返回最新数据

问题描述 投票:0回答:1

我的 flutter 应用程序使用 laravel 8。在此情况下,Api 在编辑/更新后不会返回最新数据。即使在另一个获取该数据的 api 上也是如此。

要添加数据,我遵循代码:

$input = $request->all();
            $validation = Validator::make($input, [
                // 'image' => 'required|image|max:2048',
                'user_id' => 'required',
                'team_name' => 'required',
                'description' => 'required',
                'status' => 'required',
                'members' => 'required'
            ]);

            if ($validation->fails()) {
                return response()->json([
                    'success' => false,
                    'data' => '',
                    'message' => implode(',\n', $validation->errors()->all()),
                ]);
                exit();
            }

            if (isset($request['team_name']) && !empty($request['members']) && !empty($request['description']) && !empty($request['status'])) {
                $team_url = strtolower(str_replace(' ', '_', $request['team_name']));
                $check_record = TeamModel::where('team_name', $request['team_name'])->count();
                if ($check_record > 0) {
                    /* means already exists */
                    return response()->json([
                        'success' => false,
                        'message' => 'Team name already exists',
                        'data' => []
                    ], 200);
                    die();
                }
            }

            // $name = $request->file('image')->getClientOriginalName();
            $teamModel = new TeamModel();

            // uploading image starts
            if ($request->hasFile('image')) {

                $fileName = 'teams-' . time() . '.' . $request->file('image')->getClientOriginalExtension();
                $path = $request->file('image')->move('images', $fileName);
                $teamModel->image = 'images/' . $fileName;
            }

            // uploading image ends

            // $teamModel->image = 'images/';

            $teamModel->team_name = $input['team_name'];
            $teamModel->team_url = Str::slug($request['team_name'], '_');
            $teamModel->description = $input['description'];
            $teamModel->created_by = $input['user_id'];
            $teamModel->status = $input['status'];

            if ($teamModel->save()) {

                $members  = array($input['user_id']);
                // $request['members']['owner'] = 1;
                if (!empty($request['members'])) {
                    $members = array_merge($members, $input['members']);
                }
                //Add Member
                if (!empty($members)) {
                    foreach ($members as $mem) {
                        $isSelf = $input['user_id'] == $mem ? 1 : 0;
                        // $owner = auth()->user()->id == $mem ? 1 : 0;
                        $arrData = [
                            'team_id' => $teamModel->id,
                            'user_id' => $mem,
                            'request_by' => $input['user_id'],
                            'is_accepted' => $isSelf,
                            'seen_status' => $isSelf,
                            'owner' => $isSelf
                        ];
                        DB::table('team_members')->insert($arrData);
                    }
                }

                // $teamModel->image = $base_url . $teamModel->image;
                if ($teamModel->image != null || !empty($teamModel->image))
                    $teamModel->image = $base_url . $teamModel->image;
                else
                    $teamModel->image = '';

                return response()->json([
                    'success' => true,
                    'message' => 'Team created successfully',
                    'data' => $teamModel
                ], 200);
            } else {
                return response()->json([
                    'success' => true,
                    'message' => 'Unable to create team! Please try again.',
                    'data' => $teamModel
                ], 200);
            }

更新代码:

 $id = $input['id'];

            if (!isset($input['members'])) {
                return response()->json([
                    'success' => false,
                    'message' => 'Please select team members first!',
                    'data' => []
                ], 200);
            }



            $input = $request->all();


            $validation = Validator::make($input, [
                // 'image' => 'required|image|max:2048',
                'id' => 'required',
                'user_id' => 'required',
                'team_name' => 'required',
                'description' => 'required',
                'status' => 'required',
                'members' => 'required'
            ]);

            if ($validation->fails()) {
                return response()->json([
                    'success' => false,
                    'data' => '',
                    'message' => implode(',\n', $validation->errors()->all()),
                ]);
                exit();
            }

            if (isset($request['team_name']) && !empty($request['members']) && !empty($request['description']) && !empty($request['status'])) {
                $team_url = strtolower(str_replace(' ', '_', $request['team_name']));
                $check_record = TeamModel::where('team_name', $request['team_name'])->count();
                if ($check_record > 1) {
                    /* means already exists */
                    return response()->json([
                        'success' => false,
                        'message' => 'Team name already exists',
                        'data' => []
                    ], 200);
                    die();
                }
            }


            $teamModel = TeamModel::find($id);

            $teamModel->team_name = $request['team_name'];

            $teamModel->description = $request['description'];
            $teamModel->team_url = Str::slug($request['team_name'], '_');

            // uploading image starts
            if ($request->hasFile('image')) {
                if (file_exists($teamModel->image)) {
                    unlink($teamModel->image);
                }

                $fileName = 'teams-' . time() . '.' . $request->file('image')->getClientOriginalExtension();
                $path = $request->file('image')->move('images', $fileName);
                $teamModel->image = 'images/' . $fileName;
            }

            // uploading image ends

            $teamModel->status = $request['status'];
            if ($teamModel->save()) {

                //Add Member
                $members = $input['members'];
                if (!empty($members)) {
                    foreach ($members as $mem) {
                        $isSelf = $input['user_id'] == $mem ? 1 : 0;
                        $arrData = [
                            'team_id' => $teamModel->id,
                            'user_id' => $mem,
                            'request_by' => $input['user_id'],
                            'is_accepted' => $isSelf,
                            'seen_status' => $isSelf
                        ];
                        if (!team_member($teamModel->id, $mem))
                            DB::table('team_members')->insert($arrData);
                    }
                }

                if ($teamModel->image != null || !empty($teamModel->image))
                    $teamModel->image = $base_url . $teamModel->image;
                else
                    $teamModel->image = '';

                return response()->json([
                    'success' => true,
                    'message' => 'Team updated successfully',
                    'data' => $teamModel
                ], 200);
            } else {

                return response()->json([
                    'success' => false,
                    'message' => 'Unable to update team! please try again.',
                    'data' => []
                ], 200);
            }

我使用以下方式返回响应:

return response()->json([
            'success' => true,
            'message' => 'Success message',
            'data' => $data
        ], 200);

我浏览了很多文章并尝试通过命令清除缓存,但没有一个起作用。我无法理解为什么即使在 json 上它也会获得太多缓存。

php laravel api caching laravel-8
1个回答
0
投票

我解决了这个问题。这是服务器缓存的问题。为了解决这个问题,我在另一台服务器上测试了我的代码。

© www.soinside.com 2019 - 2024. All rights reserved.