我在laravel中编写了一个API代码,在这里我的代码是在我使用护照的这个API中
class TransactionApi extends BaseController
{
public function __construct()
{
$this->middleware('client');
}
在构造函数中,我称为客户端中间件
kernel.php
protected $routeMiddleware = [
...
'client' => CheckClientCredentials::class,
];
这是我的API服务提供商
protected function mapApiRoutes()
{
Route::prefix('api/v1')
->middleware('api')
->namespace($this->namespace)
->group(__DIR__ . '/../routes/api.php');
}
API路由
Route::group(['prefix' => 'transaction'], function () {
Route::get('all', 'TransactionApi@showAllWithPaginate');
Route::get('total_refund', 'TransactionApi@totalRefund');
Route::get('total_price', 'TransactionApi@totalPrice');
Route::get('/', 'TransactionApi@show');
});
用户模型
<?php
namespace Corals\User\Models;
use Corals\Foundation\Traits\Hookable;
use Corals\Foundation\Traits\HashTrait;
use Corals\Foundation\Transformers\PresentableTrait;
use Corals\Modules\CMS\Models\Content;
use Corals\Settings\Traits\CustomFieldsModelTrait;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMediaConversions;
use Spatie\MediaLibrary\Media;
use Spatie\Permission\Traits\HasRoles;
use Yajra\Auditable\AuditableTrait;
use Corals\User\Traits\TwoFactorAuthenticatable;
use Corals\User\Contracts\TwoFactorAuthenticatableContract;
use Corals\User\Notifications\MailResetPasswordNotification;
use Corals\Modules\Subscriptions\Models\Subscription;
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable implements HasMediaConversions, TwoFactorAuthenticatableContract
{
use
TwoFactorAuthenticatable,
HasApiTokens,
Notifiable,
HashTrait,
HasRoles,
Hookable,
PresentableTrait,
LogsActivity,
HasMediaTrait,
AuditableTrait,
CustomFieldsModelTrait;
protected $slack_url = null;
/**
* Model configuration.
* @var string
*/
public $config = 'user.models.user';
protected static $logAttributes = ['name', 'email'];
protected static $ignoreChangedAttributes = ['remember_token'];
protected $casts = [
'address' => 'json',
'notification_preferences' => 'array'
];
protected $appends = ['picture', 'picture_thumb'];
protected $dates = ['trial_ends_at'];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $guarded = [
'id'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token', 'two_factor_options'
];
public function __construct(array $attributes = [])
{
$config = config($this->config);
if (isset($config['presenter'])) {
$this->setPresenter(new $config['presenter']);
unset($config['presenter']);
}
foreach ($config as $key => $val) {
if (property_exists(get_called_class(), $key)) {
$this->$key = $val;
}
}
return parent::__construct($attributes);
}
鉴于此设置,可以在本地进行测试,它将返回请求的json
但他们在登台环境中的设置相同,返回了
{
"message": "Call to undefined method Illuminate\\Auth\\GenericUser::withAccessToken()",
"exception": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError",
"file": "/path/to/vendor/laravel/passport/src/Guards/TokenGuard.php",
"line": 148,
登台的可能原因是什么
在此行
return $token ? $user->withAccessToken($token) : null;
未定义的$ user-> withAccessToken函数/方法?