php - Laravel v5.7 属于安装 Laravel Passport v7 后不返回模型
问题描述
我的代码运行良好。安装 Laravel Passport v7(这篇文章的最新版本,顺便说一句,我正在使用 Laravel v5.7 也是最新版本)后,我的代码不再工作了。当我尝试运行$user->role->id
时出现错误
试图获取非对象的属性“id”
那是因为我的$user->role
它没有返回角色模型对象,而是返回null
并且在安装 Laravel Passport 之前它返回了角色模型。我不知道是否有任何联系,但我不想更改我的整个代码,因此。即使我尝试在我的 User 模型中写入return $this->belongsTo(Role::class, 'role_id');
,但这也不起作用。
这是我的代码: 用户模型:
namespace App;
use App\Role;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function role()
{
return $this->belongsTo(Role::class);
}
}
角色模式:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
public function user()
{
return $this->hasMany(User::class);
}
}
用户表架构:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email', 250)->unique();
$table->string('password');
$table->string('phone', 11)->nullable();
$table->integer('role_id')->unsigned()->default(1);
$table->string('api_token', 60)->unique();
$table->rememberToken();
$table->timestamps();
});
角色表架构:
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
家庭控制器:
<?php
namespace App\Http\Controllers;
use App\Product;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$user = \Auth::user();
$products = new Product;
// here i am getting the error
if ($user->role_id > 2) {
$products = Product::where('name', '!=', NULL)->latest()->paginate(6);
} else {
$products = $products->getApprovedAndUntaken();
}
return view('welcome', compact('products'));
}
public function home()
{
return view('home');
}
}
解决方案
我猜这个错误与config/auth.php
.
安装护照后,去检查您的阵列是否如下所示:
配置/auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport', // Passport goes here
'provider' => 'users',
],
],
任何时候你想访问一个相关的对象,你都应该像这样访问它
$user->object()
并不是
$user->object
如果这对你有用,请让我现在!
推荐阅读
- python - 类型错误:尝试使用带有 Suds 的 Windows 身份验证 (Python NTLM) 时的预期字符串或类似字节的对象
- c++ - 函数返回零大小的字符串,即使它的大小不为零
- python - Python 3 中嵌套 For 循环的最大深度是多少?
- python - 图中的python直方图打印bin频率
- java - 无法让我的 Mismatchexception 与 switch 一起正常工作
- android - NestedScrollView(和 ScrollView)不适用于 ConstraintLayout
- html - 在 HTML 上滚动的问题
- javascript - 删除与 ng-repeat 关联的 mongodb 文档会给出“不允许在转发器中重复”
- javascript - Live Server 可以工作,但我收到 localhost 的 404 错误
- sql - 在我的 SQL 查询中,我怎样才能有一个包含从另一个表中查询的值的列?