【问题标题】:Get record by ID and all foreigns recursive in Laravel 5在Laravel 5中通过ID和所有外国人递归获取记录
【发布时间】:2015-10-04 07:21:06
【问题描述】:

我有两张表,看起来(迁移):

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 就可以做到吗?

【问题讨论】:

    标签: php mysql laravel laravel-5


    【解决方案1】:

    好的,我找到了最佳解决方案。这是嵌套集模式。我使用了baum 包,看起来:

    对于sets 表,我添加了 Baum 的列:

    Schema::create('sets', function(Blueprint $table) {
       $table->bigIncrements('id');
       $table->integer('parent_id')->nullable()->index();
       $table->integer('lft')->nullable()->index();
       $table->integer('rgt')->nullable()->index();
       $table->integer('depth')->nullable();
    
       $table->string('key');
       $table->string('name');
       $table->string('set_type');
    
       $table->timestamps();
    });
    

    完成了!只需获取 set_id 并返回帖子,例如:

    $sets = Set::where('key', '=', $myKey)->first()->getDescendantsAndSelf()->lists('id');
    $posts = Post::whereIn('set_id', $sets)->with(['user'])->take(6)->get();
    

    我将把它留给后代;)

    【讨论】:

    • 你也可以不使用foreach循环来做$set = Set::where('key', '=', $myKey)->first()->getDescendantsAndSelf()->list('id');,因为getDescendantsAndSelf()返回一个集合对象。
    • @blackpla9ue,我知道为什么,但它返回“调用未定义的方法 Baum\Extensions\Eloquent\Collection::list()”:/
    【解决方案2】:

    您需要将地区、国家和城市分解为不同的表,并建立它们之间的关系 OR 您可以在一个表中列出所有组合,即 ID |地区 |国家 |城市,这样您就可以为包含国家和地区的每个城市分别设置一行。

    【讨论】:

    • 我可以这样做,但该解决方案是有限的。例如,将来我想将社区添加到城市。我有一个问题。此外,一些国家有不同的实体,例如。美国有州,波兰有省等。这就是为什么我不能使用您指定的解决方案。
    • @Siper 如果你将实体分解成表格,那么以后很容易用县、社区等进行扩展。此外,只要它们是等价的,在不同国家叫什么县并不重要跨越国家/地区。
    猜你喜欢
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 2013-08-13
    • 1970-01-01
    • 2022-10-12
    • 2017-09-15
    相关资源
    最近更新 更多