gpt4 book ai didi

php - Eloquent 地处理多个表数据和关系

转载 作者:行者123 更新时间:2023-11-29 21:34:05 25 4
gpt4 key购买 nike

我有这 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');
});

现在,我想要一个 Eloquent 查询,它将返回一个公司数组(不仅仅是一项),其中(例如)company_clients 表上的company_id=1。另外,city_id 应该使用城市表返回名称而不是 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');
}
}

但是,我缺少 Controller 中的代码。我尝试过:

$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();

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

谢谢!

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

    $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'));

最佳答案

尝试

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

重命名

companies

CompanyClients 模型与

的关系

company

关于php - Eloquent 地处理多个表数据和关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35041448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com