首页 > 解决方案 > 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');
    }
}

标签: phplaraveleloquent

解决方案


我猜这个错误与config/auth.php.

安装护照后,去检查您的阵列是否如下所示:

配置/auth.php

 'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'passport', // Passport goes here
        'provider' => 'users',
    ],
],

任何时候你想访问一个相关的对象,你都应该像这样访问它

$user->object()

并不是

$user->object

如果这对你有用,请让我现在!


推荐阅读