Laravel/Cashier发票()返回空对象 [英] Laravel/Cashier invoices() returns empty Object

查看:43
本文介绍了Laravel/Cashier发票()返回空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

泰勒(Taylor)的收银员包裹出现了问题( https://github.com/laravel/cashier).当我尝试获取用户的发票时,返回的对象为空.有关信息,我已经将我的收银员数据库列设置到成员资格表中!然后建立适当的关系.我可以通过以下方式向用户收费:

I've got a problem with the cashier package from Taylor (https://github.com/laravel/cashier). When I try to get the Invoices for a User the Object that is returned is Empty. For information i've set up my cashier database columns into a membership table! And then set up the proper relationships. I can charge the User if I do something like this:

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

但是如果我想像这样获得发票

But if I want to get the invoices like so

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

它返回一个空的对象.

我在做什么错??

谢谢

更新:这是来自模型的代码:

Update: Here`s the code from the Models:

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或类似格式提取,这可能是从控制器返回的有用代码段:

If you are pulling it as a JSON or similar, this can be a useful snippet to return from your 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中测试

Tested in Cashier v7.x

这篇关于Laravel/Cashier发票()返回空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆