如何在 laravel 中对 auth 命令用户使用 eloquent

How to use eloquent on the auth command user in laravel

我现在遇到一个奇怪的错误

在我的控制器中,当我像这样导入 class 用户时

使用Illuminate\Foundation\Auth\User;

当我像

一样使用eloquent时它起作用了
public function index()

    {

        $farms = User::where('role_id', 3)->get();
        $user = Auth::user();
        $animal = Animal::all();
        return view('clinic.index', compact('user', 'animal', 'farms'));
    }

但在table关系方面拒绝工作,例如

public function show($id)
    {
        $farms = User::with(['animals'])->findOrFail($id);
        return view('clinic.show',compact('farms'));
    }

显示这个错误

"Call to undefined relationship [animals] on model [Illuminate\Foundation\Auth\User]"

但是每当我在控制器中将用户 class 导入为 App\User 时,

它在关系中有效,但拒绝与显示此错误的 eloquent 一起工作

"Call to a member function get() on null"

现在我有点困惑。欢迎任何帮助

App\User

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

    public static function where(string $string, int $int)
    {
    }

    public static function select(array $array)
    {
    }

    public function role(){
        return $this->belongsTo(Role::class);
    }
    public function animals(){
        return $this->hasMany(Animal::class);
    }

    public function clinics(){
        return $this->hasMany(Clinic::class);
    }

    public function slaughter(){
        return $this->hasMany(Slaughter::class);
    }

    public function address(){
        return $this->belongsTo(Address::class);
    }
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

Illuminate\Foundation\Auth\User class 是 App\User class 和 animals 关系集的父级 App\Userclass.所以你不能从 Illuminate\Foundation\Auth\User class.

调用 animals 关系

您应该从 App\User 模型中删除这些功能:

public static function where(string $string, int $int)
{
}

public static function select(array $array)
{
}