P粉9501288192023-08-21 13:53:36
As another user already mentioned, but without more code I will show you how to do it:
// 将优惠券代码存储在变量中(不需要) $couponCode = $request->couponCode; // 通过优惠券代码获取优惠券详情(直接使用first()方法,以便一次性获取模型) $coupon = Coupon::where('couponCode', $couponCode)->first(); // 在这里,您可以将模型作为JSON响应返回(在视图中使用`$data->couponName`) response()->json(['data' => $coupon]); // 或者您可以直接返回优惠券名称 return $couponDetails->couponName;
P粉8074716042023-08-21 11:54:50
The error you are getting is because the property you are trying to access does not exist in class Illuminate\Http\JsonResponse
.
You have two ways to avoid this problem:
Or return:
return $coupon->couponName;
Get data from JsonResponse class:
return $couponDetails->getData()->couponName;