Mam dwie tabele, wygląda, że (migracje):Get rekord ID i wszystkie foreigns rekurencyjnych w laravel 5
Schema::create('sets', function(Blueprint $table)
{
$table->increments('id');
$table->string('key');
$table->string('name');
$table->string('set_type');
$table->integer('belongs_to')->unsigned();
$table->timestamps();
$table->foreign('belongs_to')->references('id')->on('sets')->onDelete('cascade');
});
Schema::create('posts', function(Blueprint $table)
{
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('set_id')->unsigned();
$table->string('post_type', 25);
$table->text('post');
$table->boolean('is_reported')->default(false);
$table->boolean('is_hidden')->default(false);
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('set_id')->references('id')->on('sets');
});
„set” tabela służy do przechowywania danych, w jakiej lokalizacji (kraj, miasto ...) post powinien być widok. Na przykład, załóżmy przechowywać niektóre kraje:
id | key | name | belongs_to
1 | europe | Europe | null
2 | germany-all | Germany | 1
3 | germany-berlin | Berlin | 2
4 | germany-frankfurt | Frankfurt | 2
5 | poland-all | Poland | 1
6 | poland-warsaw | Warsaw | 5
7 | england-all | England | 1
A mój post set_id jako 6. Patrząc logicznie, kiedy chcę dostać posty z Europy (ID 1), że stanowisko powinno być zwrócone też, bo 6 należy do 5, a 5 należy do 1. I to jest to, co chcę zrobić. Można to zrobić bez używania zbyt dużej ilości PHP?
Możesz także zrobić '$ set = Set :: where ('klucz', '=', $ myKey) -> first() -> getDescendantsAndSelf() -> list ('id');' bez używania foreach do loop, ponieważ 'getDescendantsAndSelf()' zwraca obiekt kolekcji. – blackpla9ue
@ blackpla9ue Dzięki, – Siper
@ blackpla9ue, idk dlaczego, ale zwraca "Call to undefined metoda Baum \ Extensions \ Eloquent \ Collection :: list()":/ – Siper