[英]Is it possible to run a join on a variable created by a sub-query?
現在,我正在運行一個子查詢以獲取服務器的最新狀態,該子查詢通過變量last_status
返回。
//This is ran when WithLastStatusDate() is called
$query->addSubSelect('last_status', ServerStatus::select('status_id')
->whereRaw('server_id = servers.id')
->latest()
);
$servers = Server::WithLastStatusDate()
->OrderBy('servers.id', 'desc')
->where('servers.isPublic', '=', 1)
->get();
我現在想做的就是對此進行聯接,以便根據狀態表中此查詢的結果為我提供狀態的實際名稱。 我試圖做一個簡單的左聯接,但是卻收到找不到last_status列的錯誤。
$servers = Server::WithLastStatusDate()
->OrderBy('servers.id', 'desc')
->where('servers.isPublic', '=', 1)
->leftjoin('statuses','servers.last_status', '=', 'statuses.id')
->get();
誰能為我指出正確的方向呢?
編輯::
服務器表:
Schema::create('servers', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->string('url');
$table->boolean('isPublic');
$table->timestamps();
});
Server_statuses表:
Schema::create('server_statuses', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('server_id')->unsigned();
$table->foreign('server_id')->references('id')->on('servers')->onDelete('cascade');
$table->integer('status_id')->unsigned();
$table->foreign('status_id')->references('id')->on('statuses');
$table->timestamps();
});
狀態表:
Schema::create('statuses', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('key');
$table->string('status');
$table->timestamps();
});
子查詢后,$ servers是什么樣的:
查詢的原始SQL:
select `servers`.*, (select `status_id` from `server_statuses` where server_id = servers.id order by `created_at` desc limit 1) as `last_status` from `servers` where `servers`.`isPublic` = '1' order by `servers`.`id` desc
編輯2 ::
$servers = DB::table('servers as sv')
->join('server_statuses as ss', 'sv.id', '=', 'ss.server_id')
->join('statuses as st', 'ss.status_id', '=', 'st.id')
->WithLastStatus()
->OrderBy('servers.id', 'desc')
->where('servers.isPublic', '=', 1)
->get();
將LEFT JOIN與子查詢WHERE子句組合:
$servers = Server::select('servers.*', 'statuses.status as status_name')
->leftJoin('server_statuses', function($join) {
$join->on('server_statuses.server_id', '=', 'servers.id')
->where('server_statuses.id', function($query) {
$query->select('id')
->from('server_statuses')
->whereColumn('server_id', 'servers.id')
->latest()
->limit(1);
});
})
->leftJoin('statuses', 'statuses.id', '=', 'server_statuses.status_id')
->where('servers.isPublic', '=', 1)
->orderBy('servers.id', 'desc')
->get();
由於我不確定您想從查詢中得到什么,我將提供一個長解決方案並添加一些示例。 使用這些表,您應該具有以下模型:服務器模型:
class Server extends Model {
public function statuses() {
return $this->belongsToMany(Status::class, 'server_statuses');
}
}
狀態模型:
class Status extends Model {
public function servers() {
return $this->belongsToMany(Server::class, 'server_statuses');
}
}
示例:獲取服務器的最新狀態:
Server::find($serverId)->statuses()->latest()->first()->status;
獲取所有服務器狀態:
Server::find($serverId)->statuses;
獲取服務器的特定狀態:
Server::find($serverId)->statuses()->where('status', 'SomeStatus')->get();
獲取具有特定狀態的服務器:
Server::whereHas('statuses', function ($join) use ($status) {
return $join->where('status', $status);
})->get();
希望你找到答案。
據我了解,您的Server
模型和Status
模型都與ServerStatus
具有OneToMany
關系。 在這種情況下,您可以在Server
模型上偽造一個OneToOne
關系,該關系被選擇為serverStatuses()
的最新行:
class Server
{
public function serverStatuses()
{
return $this->hasMany(ServerStatus::class, 'server_id', 'id');
}
public function latestServerStatus()
{
return $this->hasOne(ServerStatus::class, 'server_id', 'id')
->latest(); // this is the most important line of this example
// `->orderBy('created_at', 'desc')` would do the same
}
}
class ServerStatus
{
public function server()
{
return $this->belongsTo(Server::class, 'server_id', 'id');
}
public function status()
{
return $this->belongsTo(Status::class, 'status_id', 'id');
}
}
class Status
{
public function serverStatuses()
{
return $this->hasMany(ServerStatus::class, 'status_id', 'id');
}
}
然后,您也可以急於加載服務器的最新狀態以及狀態本身:
Server::with('latestServerStatus.status')->get();
請注意, $server->latestServerStatus
不是集合,而是一個對象,就像在普通的OneToOne
關系中一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.