Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在应用优惠券的Laravel 5.8项目中获得折扣_Php_Laravel - Fatal编程技术网

Php 如何在应用优惠券的Laravel 5.8项目中获得折扣

Php 如何在应用优惠券的Laravel 5.8项目中获得折扣,php,laravel,Php,Laravel,我正在我的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 names

我正在我的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>

您的控制器方法有一些输入错误,请参见下面的更改

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

这个错误消息到底是从哪里来的?是否有行号或文件来缩小问题范围?@aynber确切的错误来自我的控制器代码
$优惠券代码,
是一个typo@aynber先生,我将$优惠券代码更改为$优惠券->代码,我再次收到相同的错误…请帮助我解决此问题1)不是先生,但谢谢您的礼貌。2) 请提供完整的错误消息以及行号和文件。错误消息非常模糊,如果没有这些信息,我们只能在黑暗中拍摄。感谢您的帮助,但现在我收到了对未定义方法App\User::amount()的此错误调用。请告诉我如何解决此问题以及如何在当前amount@AyushSrivastava用户模型中必须缺少方法数量,如果不查看用户模型,我就无法确定。抱歉,我在用户模型中没有看到任何方法名称amount…请像这样尝试
User::where('id',Auth::User()->id)->first()->amount金额是用户表中的一列,而不是方法,因此您必须这样使用它。
session()->put('coupon', [
   'name' => $coupon->code,
   'discount' => $coupon->discount(User::amount()),
]);