[英]Laravel eloquent orderby relation
我雄辯地從許多表中獲取數據,但是我沒有實現對我的最后一張表的排序,而是“ ordre”而不是“ id”。
我的設置:
$etages_lot = EtageLot::where('lot_id', $lot_id)->with('variantes', 'variantes.piece')->get();
EtageLot模型:
public function lot(){
return $this->belongsTo('App\Models\Copro\Lot');
}
public function etage(){
return $this->belongsTo('App\Models\Copro\Etage');
}
public function fractions()
{
return $this->hasMany('App\Models\Copro\Fraction','etage_lot_id');
}
public function variantes()
{
return $this->belongsToMany('App\Models\Copro\Variante', 'lot_variante')->withPivot('nombre');
}
瓦里安特模型:
public function piece(){
return $this->belongsTo('App\Models\Copro\Piece')->orderBy('ordre', 'asc');
}
public function EtageLot(){
return $this->belongsToMany('App\Models\Copro\EtageLot','lot_variante')->withPivot('nombre');
}
單件型號:
public function variantes(){
return $this->hasMany('App\Models\Copro\Variante');
}
我得到了所有數據,但最后一部分不是由ordre asc排序的。
我試過了 :
$etages_lot = EtageLot::where('lot_id', $lot_id)->with('etage', 'variantes')->with(['variantes.piece' => function ($query)
{
$query->orderBy('ordre','asc');
}])->get();
但這也不起作用。
為什么我不能用另一訂單獲得數據的最后一部分?
謝謝您幫忙。
編輯:添加一些遷移:
etage_lot表:
Schema::create('etage_lot', function(Blueprint $table){
$table->increments('id');
$table->integer('lot_id')->unsigned()->index();
$table->integer('etage_id')->unsigned()->index();
$table->foreign('lot_id')->references('id')->on('lots')->onDelete('cascade');
$table->foreign('etage_id')->references('id')->on('etages')->onDelete('cascade');
});
lot_variante表:
Schema::create('lot_variante', function (Blueprint $table) {
$table->increments('id');
$table->integer('etage_lot_id')->unsigned()->index();
$table->foreign('etage_lot_id')->references('id')->on('etage_lot')->onDelete('cascade');
$table->integer('variante_id')->unsigned()->index();
$table->foreign('variante_id')->references('id')->on('variantes')->onDelete('cascade');
$table->integer('nombre')->unsigned();
$table->unique(['etage_lot_id', 'variante_id']);
});
變形表:
Schema::create('variantes', function (Blueprint $table) {
$table->increments('id');
$table->string('nom')->default('sans');
$table->integer('piece_id')->unsigned()->index();
$table->foreign('piece_id')->references('id')->on('pieces')->onDelete('cascade');
$table->unique(['nom', 'piece_id']);
});
件表:
Schema::create('pieces', function (Blueprint $table) {
$table->increments('id');
$table->string('nom')->unique();
$table->string('nom_pluriel')->unique();
$table->integer('ordre')->unsigned()->unique();
$table->string('article');
$table->tinyInteger('show_article')->default(0);
$table->tinyInteger('compte_pluriel')->default(0);
$table->tinyInteger('jouissance')->default(0);
});
您可以使用修改后的withCount()
:
$etages_lot = EtageLot::where('lot_id', $lot_id)
->with(['etage', 'variantes' => function ($query) {
$query->withCount(['piece as piece_ordre' => function ($query) {
$query->select('ordre');
}])->orderBy('piece_ordre')->with('piece');
}])->get();
$etages_lot = EtageLot::with(['etage', 'variantes.piece'])->where('lot_id', $lot_id);
$etages_lot->where(function ($query) {
$query->whereHas('variantes.piece', function ($newquery) {
$newquery->orderBy('pieces.ordre',"asc")->get()->toArray();
});
});
嘗試使用這個
您可以使用whereHas
和orWhereHas
方法在您的has
查詢中放置“ where”條件。 這些方法使您可以向關系約束中添加自定義約束,例如檢查注釋的內容:
$etages_lot = EtageLot::where('lot_id', $lot_id)
->with('etage', 'variantes')
->whereHas('variantes.piece', function ($query) {
$query->orderBy('ordre', 'asc');
})->get();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.