如何雄辩地将第二张和第三张桌子连接在一起

时间:2019-01-26 10:13:20

标签: laravel eloquent eloquent--relationship

我有三个模型1.Document 2.Sharedoc 3.User

Document.php

DispatchSemaphore

Sharedoc.php

class Document extends Model
{
    public function sharedoc(){

        return $this->hasMany('App\Sharedoc','docId','id'); 

    }

    public function user(){

        return $this->belongsTo('App\User','userId','id');
    }
}

User.php

class Sharedoc extends Model
{
    public function document(){

        return $this->belongsTo('App\Document','docId','id'); 

    }
}

控制器:

class User extends Authenticatable
{
public function document(){

        return $this->hasMany('App\Document','userId','id'); 

    }
}

结果是:

$data = Document::with('sharedoc','user')
           ->where('userid',Auth::user()->id)
           ->orderBy('id','DESC')
           ->get();
return $data;

在此结果中,“文档”表与“ sharedoc”和“用户”表结合在一起…但是我希望“文档”与“ sharedoc”结合,然后“ sharedoc”与“用户”结合… 我该怎么办?

0 个答案:

没有答案