gpt4 book ai didi

Laravel/Cashier Invoices() 返回空对象

转载 作者:行者123 更新时间:2023-12-02 17:32:36 25 4
gpt4 key购买 nike

我对泰勒 ( https://github.com/laravel/cashier ) 的收银包裹有疑问。当我尝试获取用户的发票时,返回的对象为空。有关信息,我已将收银员数据库列设置到成员(member)表中!然后建立适当的关系。如果我这样做,我可以向用户收费:

$user->membership->subscription('1month')->create($token);

但是如果我想像这样拿到发票

$invoices = $user->membership->invoices();

它返回一个空对象。

我做错了什么?

谢谢

更新:这是模型中的代码:

User.php
public function membership() {
return $this->hasOne('Membership');
}

Membership.php
class Membership extends \Eloquent implements BillableInterface {

use BillableTrait;

/**
* @var array
*/
protected $dates = ['trial_ends_at', 'subscription_ends_at'];

public function user_membership() {
return $this->belongsTo('User');
}
}

最佳答案

如果您将其作为 JSON 或类似形式提取,这可能是从 Controller 返回的有用片段:

    public function index()
{
$user = auth()->user();

if($user->hasStripeId()) {
$invoices = $user->invoices()->map(function($invoice) {
return [
'date' => $invoice->date()->toFormattedDateString(),
'total' => $invoice->total,
'download' => '/user/download/invoice/' . $invoice->id,
];
});
} else {
$invoices = [];
}

return [ 'invoices' => $invoices ];
}

在 Cashier v7.x 中测试

关于Laravel/Cashier Invoices() 返回空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22423144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com