如何在Laravel中建立这种关系并访问数据

时间:2017-11-02 09:07:49

标签: php sql laravel

我有4张桌子:

表名:客户

字段:id,name,slug

表名:项目

字段:id,slug

表名:project_translation

字段:id,locale,project_id,title

表名:client_project

字段:id,client_id,project_id

关系

项目模型

public function clients()
    {
        return $this->belongsToMany(Client::class,'client_project')->withTimestamps();
        //return $this->belongsToMany('Client')->withTimestamps()->orderBy('priority', 'desc');
    }

客户端模型

public function projects()
    {
        return $this->belongsToMany(Project::class,'client_project')->withTimestamps();
    }
    public function translate()
    {
        return $this->belongsToMany(ProjectTranslation::class,'project_translations')->withTimestamps();
    }

Client_Project模型

public function clients()
    {
        return $this->hasMany('App\Models\Project');
    }
    public function projects()
    {
        return $this->hasOne('App\Models\Client');
    }   

ProjectTranslation模型

public function client()
    {
        return $this->hasMany('App\Models\Client');

    }

我试图在控制器中访问数据,如下所示:

$client_project = Client::find($id)->translate;
        return $client_project;

这给了我下一个错误:

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique 
table/alias: 'project_translations' (SQL: select 
`project_translations`.*, `project_translations`.`client_id` as 
`pivot_client_id`, `project_translations`.`project_translation_id` as 
`pivot_project_translation_id`, `project_translations`.`created_at` as 
`pivot_created_at`, `project_translations`.`updated_at` as 
`pivot_updated_at` from `project_translations` inner join 
`project_translations` on `project_translations`.`id` = 
`project_translations`.`project_translation_id` where `project_translations`.`client_id` = 22)

我不确定,但我认为关系有问题。

我在客户端刀片服务器中,我想展示该客户项目的项目翻译。

2 个答案:

答案 0 :(得分:1)

在这里,我将您的查询格式化为可读,正如我在评论中所说,我不是一个laravel用户。但我知道Sql

select 
    `project_translations`.*,
    `project_translations`.`client_id` as `pivot_client_id`,
    `project_translations`.`project_translation_id` as `pivot_project_translation_id`,
    `project_translations`.`created_at` as `pivot_created_at`,
    `project_translations`.`updated_at` as `pivot_updated_at`
from 
    `project_translations`  <-- Duplicate Table with no alias
inner join 
    `project_translations` on `project_translations`.`id` = `project_translations`.`project_translation_id`
where
    `project_translations`.`client_id` = 22

答案 1 :(得分:1)

您不需要Client_Project型号。

项目模型

public function clients()
{
    return $this->belongsToMany(Client::class,'client_project')->withTimestamps();
}

public function translates()
{
    return $this->hasMany(ProjectTranslation::class);
}

客户端模型

public function projects()
{
    return $this->belongsToMany(Project::class,'client_project')->withTimestamps();
}

ProjectTranslation模型

public function project()
{
    return $this->belongsTo('App\Models\Project');
}

在控制器中:

$client_projects = Client::find($id)->projects; //all client projects
return $client_projects;

在设置此$client_projects并在其上循环后的视图中,您可以通过以下方式获取项目的翻译:

$client_project->translates  // for single projects you will get its translates :)

对于项目,您有many to many个客户,并且one to many翻译=&gt;在文档链接中有很多例子:)