Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 拉威尔没有得到结果_Laravel_Eloquent_Laravel Eloquent_Has One - Fatal编程技术网

Laravel 拉威尔没有得到结果

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

我试图获得一个用户的角色,无论是管理员还是客户,都有一个关系,但当我试图获得详细信息并执行dd时,我得到的是假存在,有人能帮我吗

//角色迁移

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());
 }