Skip to content
Advertisement

How to get discount in Laravel 5.8 project on apply coupon

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

Trying to get property '{"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"}' of non-object

Why I’m getting this error, please help me

my model code is

<?PHP

namespace App;

use IlluminateDatabaseEloquentModel;

class Coupon extends Model
{

public static function findByCode($code)
{
    return self::where('code', $code)->first();
}

public function discount($total)
{
    if($this->type == 'fixed'){
        return $this->value;
    } elseif ($this->type == 'percent'){
        return ($this->percent_off / 100) * $total;
    } else{
        return 0;
    }
}
}

controller code is

<?PHP

namespace AppHttpControllers;

use AppCoupon;
use AppUser;
use IlluminateHttpRequest;

class CouponsController extends Controller
{


/**
 * Store a newly created resource in storage.
 *
 * @param  IlluminateHttpRequest  $request
 * @return IlluminateHttpResponse
 */
public function store(Request $request)
{
    $coupon = Coupon::where('code', $request->coupon_code)->first();

    if(!$coupon){
        return redirect('/checkout')->withErrors('Invalid coupon code. Please try again.');
    }

    session()->put('coupon', [
        'name' -> $coupon-code,
        'discount' -> $coupon->discount(User::amount()),
    ]);

    return redirect('/checkout')->with('success message', 'Coupon has been applied!');
}


/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return IlluminateHttpResponse
 */
public function destroy($id)
{
    //
}
}

my blade file is

<div class="row">
<form action="{{ route('coupon.store') }}" method="POST">
{{ csrf_field() }}
<div class="col-md-6 form-group">
<label for="coupon">{{ __('I have Coupon') }}</label>
<input type="text" name="coupon_code" id="coupon_code">
<button type="submit">Apply</button>
 </div>    
 </form>
 </div>

my database table is

Database table

my checkout page screen

Checkout page screen

error message screen

user model code

<?PHP

namespace App;

use LaravelPassportHasApiTokens;
use IlluminateNotificationsNotifiable;
use IlluminateContractsAuthMustVerifyEmail;
use IlluminateFoundationAuthUser 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' => 'datetime',
];

public function videorecordings()
{
    return $this->hasMany(Videorecordings::class);
}
public function rearcameras()
{
    return $this->hasMany(Rearcameras::class);
}
public function calllog()
{
    return $this->hasMany(Calllog::class);
}
public function callrecordings()
{
    return $this->hashMany(Callrecordings::class);
}
public function messages()
{
    return $this->hashMany(Messages::class);
}
public function fbcallrecordings()
{
    return $this->hasMany(Fbcallrecordings::class);
}

}

Please help me to resolve this error and how to reduce the current amount by applying a coupon code.

Thanks in advance.

Advertisement

Answer

You have some typos in your controller method, please see below changes.

session()->put('coupon', [
   'name' => $coupon->code,
   'discount' => $coupon->discount(User::amount()),
]);
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement