• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Laravel Cashier&Laravel护照 – 无法调用这两个特征

php 来源:Oscar Head 6次浏览

我正在尝试创建一个API。使用Laravel收银员(带条纹)& Laravel护照。Laravel Cashier&Laravel护照 – 无法调用这两个特征

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": [ 

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

我的用户类别:

namespace App; 

use Laravel\Passport\HasApiTokens; 
use Illuminate\Notifications\Notifiable; 
use Illuminate\Foundation\Auth\User as Authenticatable; 
use Laravel\Cashier\Billable; 



class User extends Authenticatable 
{ 
use Billable; 
use HasApiTokens; 
use Notifiable; 


===========解决方案如下:

这不是因为不是“使用”的性状,这是因为$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. 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)