【问题标题】:How to get discount in Laravel 5.8 project on apply coupon如何通过申请优惠券在 Laravel 5.8 项目中获得折扣
【发布时间】:2020-05-01 12:32:36
【问题描述】:

我正在我的 laravel 项目中创建一张优惠券。我需要在应用优惠券点击时允许折扣。但是当我输入我的代码并单击应用按钮时,它会显示此消息

试图获得财产 '{"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"}' 非对象

为什么会出现这个错误,请帮助我

我的型号代码是

<?PHP

namespace App;

use Illuminate\Database\Eloquent\Model;

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;
    }
}
}

控制器代码是

<?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->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 \Illuminate\Http\Response
 */
public function destroy($id)
{
    //
}
}

我的刀片文件是

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

我的数据库表是

Database table

我的结帐页面屏幕

Checkout page screen

用户模型代码

<?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' => '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);
}

}

请帮助我解决此错误以及如何通过应用优惠券代码来减少当前金额。

提前致谢。

【问题讨论】:

  • 这个错误信息究竟来自哪里?是否有行号或文件来缩小问题范围?
  • @aynber 确切的错误来自我的控制器代码
  • $coupon-code, 是一个错字
  • @aynber 先生,我再次将 $coupon-code 更改为 $coupon->code 我得到了同样的错误...请帮助我解决这个问题
  • 1) 不是先生,但感谢您的礼貌。 2) 请提供完整的错误信息以及行号和文件。错误信息非常模糊,如果没有这些信息,我们就是在黑暗中拍摄。

标签: php laravel


【解决方案1】:

您的控制器方法中有一些拼写错误,请参阅以下更改。

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

【讨论】:

  • 感谢您,但现在我收到此错误 Call to undefined method App\User::amount() 请告诉我如何解决此问题以及如何在当前金额上应用优惠券
  • @AyushSrivastava 方法数量必须从您的用户模型中丢失,如果不查看用户模型,我无法确定。
  • 抱歉,我在 User 模型中没有看到任何方法名称数量...
  • 请尝试这样User::where('id', Auth::user()-&gt;id)-&gt;first()-&gt;amount;金额是您的用户表中的一列而不是方法,因此您必须这样使用它。
猜你喜欢
  • 1970-01-01
  • 2012-12-27
  • 2015-09-08
  • 1970-01-01
  • 1970-01-01
  • 2017-11-07
  • 1970-01-01
  • 2014-07-26
  • 1970-01-01
相关资源
最近更新 更多