Filter tables according to whether it appears in the foreign value of a table

0

You see, I have an Animal table:

Schema::create('animals',function(Blueprint $table){
        $table->increments('id');
        $table->string('nombre');
        $table->string('cientifico');
        $table->string('tipo');
        $table->boolean('peligro');
        $table->timestamps();
    });

I also have a Zoo table:

Schema::create('zoos', function(Blueprint $table){
        $table->increments('id');
        $table->string('nombre');
        $table->string('pais');
        $table->string('ciudad');
        $table->integer('mc');
        $table->integer('presupuesto');
        $table->timestamps();
    });

For this table I have the following function:

public function pertenencia(){
    return $this->hasMany(Pertenencia::class);
}

And I have a table Belongs, that links both:

Schema::create('pertenencias', function(Blueprint $table){
        $table->increments('id');
        $table->unsignedInteger('zoo_id');
        $table->foreign('zoo_id')->references('id')->on('zoos');
        $table->unsignedInteger('animal_id');
        $table->foreign('animal_id')->references('id')->on('animals');
        $table->timestamps();
    });

For this last table I have the following functions:

public function animal(){
    return $this->belongsTo(Animal::class);
}

public function zoo(){
    return $this->belongsTo(Zoo::class);
}

What I want to do is filter those rows of the Zoo table that appear at least once in the Belongs table. That is, show all the zoos to which I have assigned at least one animal.

    
asked by Miguel Alparez 10.06.2018 в 13:16
source

0 answers