您现在的位置是:首页 > 经验记录>Laravel日记>laravel belongsTo和hasOne连接不同数据库的表 网站首页 Laravel日记
laravel belongsTo和hasOne连接不同数据库的表
protected $table = '要连的表名A';
//protected $table = '要连的表名C';
public function getA(){
//如果连不同库的表
return $this
->setConnection('config中database.php中的A表所在库的连接名')
->hasOne('\App\Components\控制器同名\Models\XXXX(要连表的模型名A)', 'id', 'b_id');
//如果连相同库的表
return $this
->hasOne('\App\Components\控制器同名\Models\XXXX(要连表的模型名A)', 'id', 'b_id');}
public function getC(){
//如果连不同库的表
return $this
->setConnection('config中database.php中的A表所在库的连接名')
->belongsTo('\App\Components\控制器同名\Models\XXXX(要连表的模型名C)', 'id', 'c_id');
//如果连相同库的表
return $this
->belongsTo('\App\Components\控制器同名\Models\XXXX(要连表的模型名C)', 'id', 'c_id');}
转至:https://blog.csdn.net/tclzsn7456/article/details/86581359随机推荐
文章评论
未开放






