Php 如何在Laravel 5.2中联接表?

Php 如何在Laravel 5.2中联接表?,php,mysql,laravel,eloquent,laravel-5.2,Php,Mysql,Laravel,Eloquent,Laravel 5.2,我是Laravel框架的初学者。我已经搜索了我的问题并得到了一些解决方案,但这些并不能解决我的问题。所以 配置文件表: 当任何用户创建配置文件时,我需要填写userID列。为此,我尝试编写如下代码: 配置文件模型: public function users(){ return $this->belongsTo('App\User','userID'); } public function profiles() { return $this->

我是Laravel框架的初学者。我已经搜索了我的问题并得到了一些解决方案,但这些并不能解决我的问题。所以

配置文件表: 当任何用户创建配置文件时,我需要填写
userID
列。为此,我尝试编写如下代码:

配置文件模型:

    public function users(){
        return $this->belongsTo('App\User','userID');
   }
public function profiles()
{
    return $this->hasMany('App\Profile');
}
和用户型号:

    public function users(){
        return $this->belongsTo('App\User','userID');
   }
public function profiles()
{
    return $this->hasMany('App\Profile');
}
这样,它每次都保持
null
值。我如何解决这个问题?
非常感谢advanced.

所以假设你有以下几点(你是这样做的)

或者既然我们有关系,你应该可以做这样的事情

$profile = new Profile;
$profile->somethingElse = 'abc';
// We will use the current logged in user again
Auth::user()->profiles()->save($profile); // will automagically fill in the user_id with the logged in user's id.

希望在将数据插入配置文件表时,这会给您带来一些启示,您只需执行以下操作:

$profile = new Profile();
$profile->userID = Auth::user()->id;
$profile->other_columns = 'values'
.....
$profile->save();

在此处的配置文件表中插入值时,无需连接表。希望这对您有所帮助

检查这两个都给了我错误…第一个给了我“call\u user\u func\u array()期望参数1是一个有效的回调,类'illumb\Auth\SessionGuard'没有方法'getId'”,第二个“调用未定义的方法illumb\Database\Query\Builder::save()你确定这两个功能是相互关联的吗?如果需要,你可以将它们更改为hasOne。。。如果关系不起作用,您可以使用第一个建议,分配“用户id”,因为调用save()现在我明白了。Accepted.Great:)我很高兴它对您有所帮助