2017-01-19 141 views
1

如何在雄辩的模型来表示这样的最佳方式:Laravel,雄辩多对多在透视表多对多

工作 - 多对多 - (供应商 - 多对多 - 服务)

表:

| providers | services |services_providers | jobs  | jobs_services_providers 
-------------------------------------------------------------------------------------- 
| id  | id  | id    | id  | id      | 
|   |   | providers_id  |   | services_providers_id  | 
|   |   | services_id  |   | job_id     | 
|   |   | price    |   | price      | 

谢谢!

回答

1

使用Laravel官方文件是可here你的模型应该像

型号乔布斯

class Jobs extends Model {  
    public function providers(){ 
     return $this->belongsToMany('App\Providers'); 
    }  
} 

型号提供商

class Providers extends Model { 
    public function jobs(){ 
     return $this->belongsToMany('App\Jobs'); 
    } 
    public function services(){ 
     return $this->belongsToMany('App\Services'); 
    } 
} 

标准服务

class Services extends Model {  
    public function providers(){ 
     return $this->belongsToMany('App\Providers'); 
    }  
} 

如果您在使用数据透视表,那么你可以在如果您想自定义您的字段雄辩枢关系再使用类似的模型

return $this->belongsToMany('App\Providers', 'services_providers'); 

定义的数据透视表

return $this->belongsToMany('App\Providers', 'services_providers', 'service_id', 'provider_id'); 

对于所有其他帮助你应该罚款使用LARAVEL OFFICIAL DOCUMENTATION

希望它有帮助