skip to Main Content
$couponCode = $request->couponCode;

// Get coupon details by coupon code
$coupon = Coupon::where('couponCode', $couponCode)
    ->get()
    ->first();

$couponDetails = response()->json($coupon);

return $couponDetails->couponName;

That returns as:

Undefined property: IlluminateHttpJsonResponse::$couponName (500 Internal Server Error)

I am tring to get couponName value from couponDetails

2

Answers


  1. The error you’re getting is because property you’re trying to access doesn’t exist for the class IlluminateHttpJsonResponse.

    You have two ways to avoid this:

    1. Either return:

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

      return $couponDetails->getData()->couponName;
      
    Login or Signup to reply.
  2. As another user already stated but not with more code, I will show you how to do it:

    // Store coupon code on variable (no need to)
    $couponCode = $request->couponCode;
    
    // Get coupon details by coupon code (directly use first() so you get the model in one run)
    $coupon = Coupon::where('couponCode', $couponCode)->first();
    
    // Here you can either return the model as a JSON response (and in the view you do `$data->couponName`
    response()->json(['data' => $coupon]);
    
    // Or you directly return the coupon name
    return $couponDetails->couponName;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search