雄辩的多表数据和关系

时间:2016-01-27 15:29:21

标签: php mysql laravel-5

我有这3张桌子:

  Schema::create('companies', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('city_id')->unsigned();
            $table->string('name');
            $table->string('address');
            $table->float('lat', 10,6);
            $table->float('lng', 10,6);
            $table->timestamps();

            $table->foreign('city_id')->references('id')->on('cities');
        });

Schema::create('company_clients', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('company_id')->unsigned();
    $table->integer('client_id')->unsigned();

    $table->foreign('company_id')->references('id')->on('companies');
    $table->foreign('client_id')->references('id')->on('companies');
});

Schema::create('cities', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
});

现在,我希望有一个雄辩的查询,它将返回一个公司的数组(不仅仅是一个项目),其中(例如)company_clients表上的(例如)company_id = 1。此外,city_id假设使用cities表返回名称而不是id。我无法想象现在该怎么做。 我做了:

class City extends Model
{
    protected $table = 'cities';

    public $timestamps = false;

    protected $fillable = [
        'name',
    ];


    public function companies()
    {
        return $this->hasMany('App\Company', 'city_id', 'id');
    }
} 

class CompanyClients extends Model
{
    protected $table = 'company_clients';

    public $timestamps = false;

    protected $fillable = [
        'company_id', 'client_id',
    ];

    public function companies()
    {
        return $this->belongsTo('App\Company', 'company_id', 'id');
    }

    public function clients()
    {
        return $this->belongsTo('App\Company', 'company_id', 'id');
    }
}

class Company extends Model
{
    protected $table = 'companies';

    protected $fillable = [
        'name', 'address', 'lat', 'lng', 'city_id',
    ];

    protected $hidden = [
        'clients', 'created_at', 'updated_at',
    ];

    public function city()
    {
        return $this->belongsTo('App\City', 'city_id', 'id');
    }

    public function companies()
    {
        return $this->hasMany('App\CompanyClients', 'company_id', 'id');
    }

    public function clients()
    {
        return $this->hasMany('App\CompanyClients', 'client_id', 'id');
    }
}

但是,我错过了控制器中的代码。我试过了:

$result = Company::leftJoin('company_clients', function($join) {
            $join->on('companies.id', '=', 'company_clients.company_id');
        })->where('company_clients.company_id', '=', 1 )->get();

    $result = Company::with(['clients' => function($q){
        $q->where('company_id', 1);
    }])->get();

但未返回正确的结果。我缺少什么?

谢谢!

EDITED: 我找到了一种方法,但我不确定这是否是最好的方法。有人可以确认吗?

    $result = Company::join('company_clients', function($join) {
        $user = Auth::guard('api')->user();
        $join->on('companies.id', '=', 'company_clients.client_id')->where('company_clients.company_id', '=', $user->company_id );
    })->join('cities', 'cities.id', '=', 'companies.city_id')->get(array('companies.*', 'cities.name'));

1 个答案:

答案 0 :(得分:0)

尝试

CompanyClients::with('company.city', 'clients')->where('company_id', 1)->get();

重命名

  

公司

将CompanyClients模型与

的关系
  

公司