简体   繁体   English

Laravel 5.4 Auth :: User()与关系不起作用

[英]Laravel 5.4 Auth::User() with relationship not working

I have Table Vendors (for test Auth relationship) and Table Vendor_users, 我有Table Vendors(用于测试Auth关系)和Table Vendor_users,

But i use Auth::user() it not relationship 但是我使用Auth::user()它不是关系

在此处输入图片说明

And This Database 和这个数据库

在此处输入图片说明

In Vendor model 在供应商模型中

    protected $table = 'vendor_users';
 public function Vendor_test(){
        return $this->belongsTo(Vendor_test::class);
    }

And Vendor_test model 和Vendor_test模型

 protected $table = 'vendors';

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

from chat and your current table structure, you should have relation like this 从聊天和您当前的表结构中,您应该具有这样的关系

in Vendor Model 在供应商模型中

public function vendor_contact() 
{ 
  return $this->belongsTo(Vendor_contact::class, 'vendor_contact_id'); 
}

in Vendor_contact Model 在Vendor_contact模型中

protected $primaryKey = 'vendContactId'; //check this 

public function vendor() 
{ 
  return $this->hasOne(Vendor::class, 'vendor_contact_id'); 
}

Now use lazy eager loading for loading vendor_contact relationship 现在使用懒惰的渴望加载来加载vendor_contact关系

Auth::user()->load('vendor_contact');
dd(Auth::user());

As per the discussion and the table structure you have, Add the relation function in your model vendor_users . 根据讨论内容和表结构,在模型vendor_users添加关系函数。

protected $table = 'vendor_users';
public function vendor_contact() 
{ 
   return $this->belongsTo(Vendor_contact::class, 'vendor_contact_id'); 
} 

get the user with the vendor_contact and check 使用户与vendor_contact并检查

$user = Auth::user()->with('vendor_contact')->first(); //  As you asked with for auth
//OR
$user = Auth::user()->load('vendor_contact'); // From the rkj answer as I found this good.
// OR
$user = Vendor::find(1)->with('vendor_contact')->first(); 
dd($user);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM