The value of $couponDetails->couponName cannot be returned in Laravel
P粉714890053
P粉714890053 2023-08-20 21:26:59
0
3
531
<p><br /></p> <pre class="brush:php;toolbar:false;">$couponCode = $request->couponCode; // Get coupon details through discount code $coupon = Coupon::where('couponCode', $couponCode) ->get() ->first(); $couponDetails = response()->json($coupon); return $couponDetails->couponName; </pre> <p>The return result is as follows:</p> <blockquote> <p>Undefined property: IlluminateHttpJsonResponse::$couponName (500 Internal Server Error)</p> </blockquote> <p>I am trying to get the value of couponName from couponDetails</p>
P粉714890053
P粉714890053

reply all(2)
P粉950128819

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粉807471604

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:

  1. Or return:

    return $coupon->couponName;
    
  2. Get data from JsonResponse class:

    return $couponDetails->getData()->couponName;
    
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!