Laravel Cashier& Laravel Passport - 无法调用两个特征

时间:2017-12-03 06:08:18

标签: php laravel

我正在尝试创建一个API。使用Laravel Cashier(带条纹)& Laravel护照。

var d = new Date("2017-10-03");
d.setMonth(d.getMonth() + 1);
var month = d.getMonth(); // get new month
alert(month);

我在这里遗失了什么吗?我无法从用户调用Billable模型特征。在我的控制器中,它显示在Authentiable中找不到newSubscription而不是Billable。

我的用户类:

    public $successStatus = 200;
public function index(Request $request)
{
    $validator = Validator::make($request->all(), [
        'name' => 'required',
        'email' => 'required|email',
        'password' => 'required',
        'c_password' => 'required|same:password',
    ]);
    if ($validator->fails()) {
        return response()->json(['error' => $validator->errors()], 401);
    }
    $input = $request->all();
    $input['password'] = bcrypt($input['password']);
    $user = User::create($input);
    $user = Auth::user();
    $success['token'] = $user->createToken('MyApp')->accessToken;
    $success['name'] = $user->name;
    $success['email'] = $user->email;
    $stripeToken = 'tok_visa';
    \Stripe\Stripe::setApiKey(env(STRIPE_SECRET));

    try
    {
        $user->newSubscription('monthly')->create($stripeToken, [
            'email' => $user->email, 'description' => 'Our First Customer'
        ]);

        return response()->json(['success' => $success], $this->successStatus);
    }
    catch(Exception $e)
    {
        return response()->json(['success' => $success], $this->successStatus);
    }

"message": "Call to a member function createToken() on null",
"exception": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError",
"file": "/Users/Taylor/Documents/Websites/storingcompany/api/app/Http/Controllers/Register.php",
"line": 32,
"trace": [

1 个答案:

答案 0 :(得分:1)

这不是因为没有'使用'特征,这是因为$user变量是null

您有一行代码可在数据库中创建新的用户记录。紧接着的下一行,您正在尝试获取当前经过身份验证的用户。我认为这是一些注册或注册过程,因此不会有经过身份验证的用户,因为它们尚不存在于系统中。

$user = User::create(...);
$user = Auth::user();  // what do you expect this to be, besides `null`

$user->anyMethodEver(); // Call to a member function ... on null.