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



我现在面临一个奇怪的错误

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

使用Illuminate\Foundation\Auth\User;

当我像一样使用雄辩时,它就起作用了

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

但当涉及到像这样的表关系时,它拒绝工作

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

给我看这个错误

"Call to undefined relationship [animals] on model [IlluminateFoundationAuthUser]"

但每当我在我的控制器中将用户类导入为App\user时

它在关系中起作用,但拒绝与雄辩的显示此错误一起工作

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

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

应用程序\用户

<?php
namespace App;
use IlluminateContractsAuthMustVerifyEmail;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;
use IlluminateDatabaseEloquentModel;
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',
];
}

IlluminateFoundationAuthUser类是AppUser类和AppUser类中animals关系集的父类。所以不能从IlluminateFoundationAuthUser类调用animals关系。

您应该从AppUser型号中删除这些功能:

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

最新更新