2

所以我想要属于正确关系的相应项目,所以当类型是App\\Models\\Post它应该得到一个帖子项目,如果类型是App\\Models\\Comment它应该得到一个评论等等

查询

$result = $bookmark->items()
        ->where(function ($query) {
            $query
                ->where(function ($query) {
                    $query->where('bookmark_item_type', 'App\\Models\\Post')->With('posts');
                })
                ->orWhere(function ($query) {
                    $query->where('bookmark_item_type', 'App\\Models\\Comment')->With('comments');
                });
        })
        ->get()
        ->toArray();

结果

     0: {id: 1, bookmark_item_id: 2, bookmark_item_type: "App\Models\Post", bookmarks_id: 1, created_at: null,…}
     bookmark_item_id: 2
     bookmark_item_type: "App\\Models\\Post"
     bookmarks_id: 1
     created_at: null
     id: 1
     updated_at: null

     1: {id: 28, bookmark_item_id: 12, bookmark_item_type: "App\Models\Comment", bookmarks_id: 1,…}
     bookmark_item_id: 12
     bookmark_item_type: "App\\Models\\Comment"
     bookmarks_id: 1
     created_at: null
     id: 28
     updated_at

评论或帖子甚至不会出现在结果中。

编辑:

我最终得到了这个,但如果这可以在 1 个查询中完成,那仍然会很好!所以我可以添加日期过滤器DESCASC过滤器。

    $posts = $bookmark->items()
        ->where("bookmark_item_type", 'App\\Models\\Post')
        ->with('posts')
        ->get()
        ->toArray();

    $comments = $bookmark->items()
        ->where("bookmark_item_type", 'App\\Models\\Comment')
        ->with('comments')
        ->get()
        ->toArray();

更多编辑!

我看过这篇文章Laravel - Eager Loading Polymorphic Relation's Related Models但是当我将这样的关系添加到我的模型中时

protected $with = [
    'posts',
    'comments',
];

在此处输入图像描述

它仍然使用帖子类型加载评论,反之亦然,模型我错过了什么吗?或者我可以在带有额外过滤器的查询中添加一些东西吗?每个单独的项目?因为我无法获取数据,$bookmarkitem->bookmark_item因为我使用 ajax 来获取数据请求,所以我不能在前端这样做来获取我的帖子或评论

楷模:

class BookmarkItem extends Model
{

    public function posts(): \Illuminate\Database\Eloquent\Relations\MorphToMany
    {
        return $this->morphedByMany(Post::class, 'bookmark_item');
    }


    public function comments(): \Illuminate\Database\Eloquent\Relations\MorphToMany
    {
        return $this->morphedByMany(Comment::class, 'bookmark_item');
    }
}

class Post extends Model
{
    public function bookmarkItem()
    {
        return $this->morphToMany(BookmarkItem::class, 'bookmark_item');
    }
}

class Comment extends Model
{
    public function bookmarkItem()
    {
        return $this->morphToMany(BookmarkItem::class, 'bookmark_item');
    }
}

数据库的 DB Schemes

Schema::create('bookmark_items', function (Blueprint $table) {
        $table->id();
        $table->integer('bookmark_item_id')->unsigned();
        $table->string('bookmark_item_type');
        $table->unsignedBigInteger('bookmarks_id');
        $table->foreign('bookmarks_id')->references('id')->on('bookmarks');
        $table->timestamps();
    });

Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->string("title");
        $table->string("uuid")->unique();
        $table->string("image_path");
        $table->LongText("description")->nullable();
        $table->unsignedBigInteger('user_id');
        $table->foreign('user_id')->references('id')->on('users');
        $table->timestamps();
    });

Schema::create('comments', function (Blueprint $table) {
        $table->id();
        $table->integer('user_id')->unsigned();
        $table->integer('parent_id')->unsigned()->nullable();
        $table->text('comment');
        $table->integer('commentable_id')->unsigned();
        $table->string('commentable_type');
        $table->timestamps();
    });

Schema::create('bookmarks', function (Blueprint $table) {
        $table->id();
        $table->string("type")->default("default");
        $table->unsignedBigInteger('user_id');
        $table->foreign('user_id')->references('id')->on('users');
        $table->timestamps();
    });
4

0 回答 0