提问人:sard siders 提问时间:11/15/2023 更新时间:11/15/2023 访问量:48
Laravel Follow Unfollow 系统不适用于 Flutter 前端
laravel follow unfollow system not working with flutter front end
问:
我正在使用 Laravel 作为我的 flutter 移动应用程序的后端,但对关注和取消关注系统有一些问题。
这些是我的 Laravel 代码:
用户模型:
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
protected $fillable = [
'name',
'phonenumber',
'image',
'password',
'usermoney',
'vipuser',
'userphonenumber',
'job',
'star',
'codemelli',
'bio',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'phonenumber_verified_at' => 'datetime',
'password' => 'hashed',
];
public function followings()
{
return $this->belongsToMany(User::class, 'follows', 'follower_id', 'following_id');
}
public function followers()
{
return $this->belongsToMany(User::class, 'follows', 'following_id', 'follower_id');
}
public function is_following($id)
{
return $this->followings()->where('following_id', $id)->exists();
}
}
这是我的 AuthController 的夏天:
public function is_following(User $follower, User $following)
{
return response()->json([
'status' => $follower->is_following($following->id),
'message' => 'followed.',
], 200);
}
这是我的 API 路由:
Route::get('/status/{follower}/{following}', [AuthController::class, 'is_following']);
这是我为该函数进行的迁移类:
return new class extends Migration
{
public function up(): void
{
Schema::create('follows', function (Blueprint $table) {
$table->unsignedBigInteger('follower_id');
$table->unsignedBigInteger('following_id');
$table->unique(['following_id', 'follower_id']);
$table->foreign('follower_id')
->references('id')
->on('users')
->cascadeOnDelete();
$table->foreign('following_id')
->references('id')
->on('users')
->cascadeOnDelete();
$table->timestamp('created_at');
});
}
public function down(): void
{
Schema::dropIfExists('follows');
}
};
现在我将分享我的前端颤振代码:
服务部分:
Future<ApiResponse> followunfollow(User follower, User following) async {
ApiResponse apiResponse = ApiResponse();
try {
String token = await getToken();
final response = await http
.get(Uri.parse('$userURL/is_following/$follower/$following'), headers: {
'Accept': 'application/json',
'Authorization': 'Bearer $token'
});
switch (response.statusCode) {
case 200:
apiResponse.data = jsonDecode(response.body)['message'];
break;
case 401:
apiResponse.error = unauthorized;
break;
default:
apiResponse.error = somethingWentWrong;
break;
}
} catch (e) {
apiResponse.error = serverError;
}
return apiResponse;
}
在我的代码中,我想使用它的地方在这里: (用户,用户是用户类型的相同实例,我检查过它们是否正常)
void fUNf() async {
ApiResponse response = await followunfollow(user, user);
if (response.error == null) {
setState(() {});
} else if (response.error == unauthorized) {
logout().then((value) => {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) => Login()),
(route) => false)
});
} else {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('${response.error}')));
}
}
最后,我创建按钮,按下它会像:
onTap: () {
fUNf();
},
当我点击按钮时出现的错误是在我的 flutter 服务部分定义的:
apiResponse.error = somethingWentWrong; //this is a part of services part in my flutter code
无论如何,我每次都会遇到这个错误!
答: 暂无答案
评论