【发布时间】: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>
我的数据库表是
我的结帐页面屏幕
用户模型代码
<?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) 请提供完整的错误信息以及行号和文件。错误信息非常模糊,如果没有这些信息,我们就是在黑暗中拍摄。