API 不返回 Laravel 8 中的最新数据
原标题:API not returning latest data in laravel 8
I am using laravel 8 for my flutter app. In this, Api s are not returning latest data after edit/update. Even on another api of getting that data.
To add data I am following code:
$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( ,
, $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);
}
To update code:
$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( ,
, $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);
}
I am using following way to return response:
return response()->json([
success => true,
message => Success message ,
data => $data
], 200);
I have explored many articles and tried clearing cache by commands but none of them is worked. I am not able to understand why it s getting too much cache even on json.
问题回答
I solved this problem. It was the cache issue from the server. To figure out this issue, I tested my code on another server.