Laravel雄辩的顺序关系

时间:2018-07-20 10:39:56

标签: php laravel

我雄辩地从许多表中获取数据,但是我无法实现最后一张表的排序,而是“ 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');
}

Variante模型:

    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']);
    });

variants表:

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

    });

3 个答案:

答案 0 :(得分:1)

您可以使用修改后的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();

答案 1 :(得分:0)

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

尝试使用此

答案 2 :(得分:0)

您可以使用whereHasorWhereHas方法在has查询中添加“ where”条件。这些方法使您可以向关系约束中添加自定义约束,例如检查注释的内容:

$etages_lot = EtageLot::where('lot_id', $lot_id)
 ->with('etage', 'variantes')
 ->whereHas('variantes.piece', function ($query) {
    $query->orderBy('ordre', 'asc');
 })->get();