mardi 5 mars 2019

Laravel5.5 Eloquent Relationships with() no effect

I have 3 model, and I already set Relation.

Model A:

namespace App\Model;
use Illuminate\Database\Eloquent\Model;

class ComponentA extends Model
{
    protected $table = 'ComponentA';
    protected $primaryKey = 'ComponentAId';

    public function ComponentB() {
        return $this->hasOne(
            'App\Model\ComponentB', 'ComponentBId', 'ComponentBId'
        );
}

Model B:

namespace App\Model;
use Illuminate\Database\Eloquent\Model;

class ComponentB extends Model
{
    protected $table = 'ComponentB';
    protected $primaryKey = 'ComponentBId';

    public function ComponentC() {
        return $this->hasOne(
            'App\Model\ComponentC', 'ComponentCId', 'ComponentCId'
        )->withDefault();
    }
}

Model C:

namespace App\Model;
use Illuminate\Database\Eloquent\Model;

class ComponentC extends Model
{
    protected $table = 'ComponentC';
    protected $primaryKey = 'ComponentCId';
}

Now, in the controller, I want to call C from A and sort by C column, I use with() method.

Controller:

$this->ComponentA->with(['ComponentB.ComponentC' => function($query) {
    $query->orderby('ComponentCId','DESC');
}])->get();

That is correct, no any error, but the result doesn't sort.

Please help me that where I am wrong ?

Thanks a lot.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire