title: How to get discount in Laravel 5.8 project on apply coupon
body: <p>I'm creating a coupon in my laravel project. I need to allow a discount on applying coupon click. but when I enter my code and click on an apply button it gives this message</p>
  
  <blockquote>
    <p>Trying to get property
    <code>'{"id":1,"code":"ABC123","type":"fixed","value":30,"percent_off":null,"created_at":"2020-04-29 07:30:14","updated_at":"2020-04-29 08:34:21"}'</code> of non-object</p>
  </blockquote>
  
  <p>Why I'm getting this error, please help me</p>
  
  <p><strong>my model code is</strong></p>
  
  <pre><code>&lt;?PHP
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Coupon extends Model
  {
  
  public static function findByCode($code)
  {
      return self::where('code', $code)-&gt;first();
  }
  
  public function discount($total)
  {
      if($this-&gt;type == 'fixed'){
          return $this-&gt;value;
      } elseif ($this-&gt;type == 'percent'){
          return ($this-&gt;percent_off / 100) * $total;
      } else{
          return 0;
      }
  }
  }
  </code></pre>
  
  <p><strong>controller code is</strong></p>
  
  <pre><code>&lt;?PHP
  
  namespace App\Http\Controllers;
  
  use App\Coupon;
  use App\User;
  use Illuminate\Http\Request;
  
  class CouponsController extends Controller
  {
  
  
  /**
   * Store a newly created resource in storage.
   *
   * @param  \Illuminate\Http\Request  $request
   * @return \Illuminate\Http\Response
   */
  public function store(Request $request)
  {
      $coupon = Coupon::where('code', $request-&gt;coupon_code)-&gt;first();
  
      if(!$coupon){
          return redirect('/checkout')-&gt;withErrors('Invalid coupon code. Please try again.');
      }
  
      session()-&gt;put('coupon', [
          'name' -&gt; $coupon-code,
          'discount' -&gt; $coupon-&gt;discount(User::amount()),
      ]);
  
      return redirect('/checkout')-&gt;with('success message', 'Coupon has been applied!');
  }
  
  
  /**
   * Remove the specified resource from storage.
   *
   * @param  int  $id
   * @return \Illuminate\Http\Response
   */
  public function destroy($id)
  {
      //
  }
  }
  </code></pre>
  
  <p><strong>my blade file is</strong></p>
  
  <pre><code>&lt;div class="row"&gt;
  &lt;form action="{{ route('coupon.store') }}" method="POST"&gt;
  {{ csrf_field() }}
  &lt;div class="col-md-6 form-group"&gt;
  &lt;label for="coupon"&gt;{{ __('I have Coupon') }}&lt;/label&gt;
  &lt;input type="text" name="coupon_code" id="coupon_code"&gt;
  &lt;button type="submit"&gt;Apply&lt;/button&gt;
   &lt;/div&gt;    
   &lt;/form&gt;
   &lt;/div&gt;
  </code></pre>
  
  <p><strong>my database table is</strong></p>
  
  <p><a href="https://i.stack.imgur.com/H9JI9.png" rel="nofollow noreferrer">Database table</a></p>
  
  <p><strong>my checkout page screen</strong></p>
  
  <p><a href="https://i.stack.imgur.com/04KLG.png" rel="nofollow noreferrer">Checkout page screen</a></p>
  
  <p><a href="https://i.stack.imgur.com/kBf2U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kBf2U.png" alt="error message screen"></a></p>
  
  <p><strong>user model code</strong></p>
  
  <pre><code>&lt;?PHP
  
  namespace App;
  
  use Laravel\Passport\HasApiTokens;
  use Illuminate\Notifications\Notifiable;
  use Illuminate\Contracts\Auth\MustVerifyEmail;
  use Illuminate\Foundation\Auth\User as Authenticatable;
  
  
  class User extends Authenticatable
  {
  use Notifiable, HasApiTokens;
  
  /**
   * The attributes that are mass assignable.
   *
   * @var arra
   */
  protected $fillable = [
      'name', 'email', 'password', 'phone', 'country', 'state', 'purpose', 'package', 'months', 'quantity', 'amount', 'expiry_date', 'last_login_at', 'last_login_ip',
  ];
  
  
  
  /**
   * The attributes that should be hidden for arrays.
   *
   * @var array
   */
  protected $hidden = [
      'password', 'remember_token',
  ];
  
  /**
   * The attributes that should be cast to native types.
   *
   * @var array
   */
  protected $casts = [
      'email_verified_at' =&gt; 'datetime',
  ];
  
  public function videorecordings()
  {
      return $this-&gt;hasMany(Videorecordings::class);
  }
  public function rearcameras()
  {
      return $this-&gt;hasMany(Rearcameras::class);
  }
  public function calllog()
  {
      return $this-&gt;hasMany(Calllog::class);
  }
  public function callrecordings()
  {
      return $this-&gt;hashMany(Callrecordings::class);
  }
  public function messages()
  {
      return $this-&gt;hashMany(Messages::class);
  }
  public function fbcallrecordings()
  {
      return $this-&gt;hasMany(Fbcallrecordings::class);
  }
  
  }
  </code></pre>
  
  <p>Please help me to resolve this error and how to reduce the current amount by applying a coupon code.</p>
  
  <p>Thanks in advance.</p>