按 ID 获取记录,所有外国人在 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”表用于存储应查看帖子的位置(国家/地区,城市...)的数据。例如,让我们存储一些国家::
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
而且,我的帖子set_id为6。从逻辑上讲,当我想从欧洲(ID 1)获取帖子时,该帖子也应该返回,因为6属于5,5属于1。这就是我想做的。可以在不使用太多PHP的情况下完成吗?