【问题标题】:Load All posts with their tags into an array Laravel eloquent polymorphic relationship将所有带有标签的帖子加载到数组中 Laravel 雄辩的多态关系
【发布时间】:2020-05-06 08:35:49
【问题描述】:

我需要将表中存在的所有帖子及其所有标签提取到一个数组中。正如您在下面看到的,此查询仅获取具有标签的帖子,但我需要所有帖子,即使它们没有任何标签。此外,我的数组中需要有一个tag:[] 变量,但我不知道如何在一个查询中获取所有带有标签的帖子。

提前谢谢你。

public function getItemsWithTags(array $attr)
{
        return self::$model::
        whereNotNull('title')->
        whereNotNull('content')->
        whereNotNull('author')->
        whereNotNull('category')->
        whereNotNull('img_url')->
        has('tags')->
        get()->
        toArray();
}

class BaseModel extends Model
{
    protected $table;
    protected $primaryKey;


    use SoftDeletes;

}

class BlogModel extends BaseModel
{

    protected $table = 'blog';
    protected $primaryKey = 'id';


    public function tags()
    {
        return $this->morphToMany(TagsModel::class,'taggable',null,null,'tag_id');
    }
}


class TagsModel extends BaseModel
{

    protected $table = 'tags';
    protected $primaryKey = 'id';

    protected $fillable = ['name'];
    public function blog()
    {
        return $this->morphedByMany(BlogModel::class,"taggable");
    }


}

此查询的输出:


{
            "id": 28,
            "title": "dfg",
            "content": "<ul>\n<li>rdgrdgf</li>\n<li>dfg</li>\n<li>dg</li>\n<li>dgf</li>\n<li>dfg</li>\n</ul>",
            "author": "gd",
            "category": "Design",
            "img_url": "uploads/post/Screenshot from 2020-03-27 20-34-42_7dc41dca1ebc4dabcb921fc7f4c4744a.png",
            "created_at": "2020-05-03T18:47:38.000000Z",
            "updated_at": "2020-05-03T18:47:38.000000Z",
            "deleted_at": null,
            "user_id": 1,
            "category_id": 7
        }


而我需要的是:

{
            "id": 28,
            "title": "dfg",
            "content": "<ul>\n<li>rdgrdgf</li>\n<li>dfg</li>\n<li>dg</li>\n<li>dgf</li>\n<li>dfg</li>\n</ul>",
            "author": "gd",
            "category": "Design",
            "img_url": "uploads/post/Screenshot from 2020-03-27 20-34-42_7dc41dca1ebc4dabcb921fc7f4c4744a.png",
            "created_at": "2020-05-03T18:47:38.000000Z",
            "updated_at": "2020-05-03T18:47:38.000000Z",
            "deleted_at": null,
            "user_id": 1,
            "category_id": 7,
            "tags":['tag1','tag2']
        }


【问题讨论】:

  • 你可以使用with的方法,把has('tags')改成with('tags')
  • 亲爱的@Merijndk,感谢您的评论,但它检索到了一个空的标签数组
  • 你查询的模型有tags方法吗?
  • 是的,我编辑了我的问题并添加了我的模型,你可以看到它

标签: php laravel eloquent polymorphic-associations


【解决方案1】:
return self::$model::
    with('tags')->
    whereNotNull('title')->
    whereNotNull('content')->
    whereNotNull('author')->
    whereNotNull('category')->
    whereNotNull('img_url')->
    get()->
    toArray();

【讨论】:

  • 虽然此代码 sn-p 可能是解决方案,但包含解释确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
【解决方案2】:

顾名思义,你使用has,这个方法检查是否有标签关系,用with代替:

public function getItemsWithTags(array $attr)
{
        return self::$model::
        whereNotNull('title')->
        whereNotNull('content')->
        whereNotNull('author')->
        whereNotNull('category')->
        whereNotNull('img_url')->
        with('tags')->
        get()->
        toArray();
}

【讨论】:

    猜你喜欢
    • 2020-09-17
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 2018-06-01
    相关资源
    最近更新 更多