Laravel 拉威尔没有得到结果
我试图获得一个用户的角色,无论是管理员还是客户,都有一个关系,但当我试图获得详细信息并执行dd时,我得到的是假存在,有人能帮我吗 //角色迁移Laravel 拉威尔没有得到结果,laravel,eloquent,laravel-eloquent,has-one,Laravel,Eloquent,Laravel Eloquent,Has One,我试图获得一个用户的角色,无论是管理员还是客户,都有一个关系,但当我试图获得详细信息并执行dd时,我得到的是假存在,有人能帮我吗 //角色迁移 public function up() { Schema::create('roles', function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger('user_id'); $tab
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->enum('role',['Club Manager','Admin','Mc']);
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}
//用户模型
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use \App\Role;
class User extends Authenticatable
{
use 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 null;
}
public function getrole(){
$role=$this->hasMany('App\Role');
return $role;
}
public function getuserRole(){
return Role::where('user_id',$this->id)->first();
}
}
//Controller
public function getrole(){
dd(Auth::user()->getrole());
}
您必须将关系作为属性而不是方法进行访问:
Auth::user()->getrole;
您必须将关系作为属性而不是方法进行访问:
Auth::user()->getrole;
显示您的控制器或查看代码如何调用function@MaheshBhattarai我已经编辑了这篇文章,按照建议添加了控制器,我正在尝试学习laracelShow你的控制器或查看代码如何称呼它function@MaheshBhattarai我已经编辑了这篇文章,按照建议添加了控制器,我正在努力学习拉拉塞尔
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use \App\Role;
class User extends Authenticatable
{
use 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 null;
}
public function getrole(){
$role=$this->hasMany('App\Role');
return $role;
}
public function getuserRole(){
return Role::where('user_id',$this->id)->first();
}
}
//Controller
public function getrole(){
dd(Auth::user()->getrole());
}