一、一对一关联 belongsTo
1.路由
Route::get('yhj','Yhj/yhj')->allowCrossDomain();
2.控制器
public function yhj(){
//连接的模型::with(['方法名1','方法名2'])->select()->toArray();
$data = \app\home\model\Yhj::with(['types','paths'])->select()->toArray();
return success($data);
}
3.模型层
class Yhj extends Model
{
//主表id
protected $pk='cid';
//主表
protected $table='coupon';
//关联表1
public function types(){
//belongTo(关联模型,'外键','主键')->bind('名称字段')
return $this->belongsTo(Type::class,'type_id','type_id')->bind(["type_name"]);
}
//关联表2
public function paths(){
return $this->belongsTo(Path::class,'path_id','