где на вложенном - PullRequest
       12

где на вложенном

0 голосов
/ 20 октября 2019

я хочу получить местоположение со всеми его disponibilites, у которых не было etat = "архиватор"

Disponibilite

class Disponibilite extends Model
{
    public function etat()
    {
        return $this->belongsTo('App\Etat');
    }
}

местоположение

class location extends Model
{
    public function disponibilite()
    {
        return $this->hasMany('App\Disponibilite');
    }
}

etatпусто

class Etat extends Model
{
}

это моя попытка

$location = location::where("id", $id)
->with([
    "locataire",
    "prix",
    "acceptations",
    "distances",
    "equipements",
    "logement",
    "language",
    "photo",
    "disponibilite" => function ($query) {
        $query->with(["etat" => function ($q) {
            $q->where("type", "!=", "archiver");
        }]);
    }
])
->first();

Ответы [ 2 ]

1 голос
/ 20 октября 2019

я исправил это, проблема в том, что в некоторых рядах disponibilites есть etat (NULL), поэтому, если я проверял тип etat (null) в функции обратного вызова, ничего не произойдет, поэтому я добавляю OrWhere ("etat_id", null) и я поменял с withhas

$location = location::with(
                [
                    "locataire",
                    "prix",
                    "acceptations",
                    "distances",
                    "equipements",
                    "logement",
                    "language",
                    "photo",
                    "disponibilite" => function ($query) {

                        $query->whereHas("etat", function ($q) {

                            $q->where("type", "!=", "archiver");
                        })->Orwhere("etat_id", null);
                    }
                ]
            )
                ->find($id);
1 голос
/ 20 октября 2019
$location = location::with([
    "locataire",
    "prix",
    "acceptations",
    "distances",
    "equipements",
    "logement",
    "language",
    "photo",
    "disponibilite" => function ($disponibilite) {
        // only get the 'disponibilites' that has the following relationship
        $disponibilite->whereHas("etat", function ($etat) {
            $etat->where("type", "!=", "archiver");
        })
        // eager load the relationship
        ->with(["etat" => function ($etat) {
            $etat->where("type", "!=", "archiver");
        }]);
    }
])
// This is the same as using where('id', $id)->first()
->find($id);
...