在Laravel V5.8中使用形态关系加载嵌套关系

时间:2019-03-11 10:36:48

标签: php laravel lumen

嗨,我正在处理laravel v5.7形态关系。现在我被困在如何挑选postable_type模型的选定关系上。

private static function getHomeQuery($user_id) {
        $query = PostBox::distinct("user_posts_id")->select("user_posts_id")
                ->where(function($sql) {
                    $sql->where('archive', '=', false);
                    $sql->where('status', '=', 'A');
                })
                ->whereHas('post', function($sql) {
                    $sql->where('postable_type', 'media');
                })
                ->with(["post" => function($sql) use($user_id) {
                $columns = ["id", "text_content", "created_at",
                    "local_db_path", "user_id", "post_type_id", "web_url",
                    "location_id", "short_code", \DB::raw("id as postable_id,postable_type")];
                $sql->select($columns);
                $sql->with(['user' => function($sql) {
                        $sql->select('id', "uid", 'username', 'picture', 'bucket', 'is_live');
                    }, 'postable', 'postLikesByUser' => function($sql) use ($user_id) {
                        $sql->select('id', 'liked_to');
                        $sql->where("liked_by", "=", $user_id);
                    }]);
            }]);
        return $query;
    }

这是我需要修改的查询。 职位关系是我的形态模型,运作良好。当我运行此查询时,此查询返回当前表记录和形态表记录。但是在形态表中,我有post_media模型,其中包含至少10个进一步的关系。现在,我只想选择与此查询执行相关的三个关系。贝洛(Belo)是我的PostMedia模型,我只想从中选择所选关系。

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MediaPost extends Model {

    protected $primarykey = 'id';

    protected $table = 'media_posts';

    protected $with = ["postMedia"];

    protected $fillable = [
        'id', 'post_type_id', 'user_id', 'archive', 'created_at', 'updated_at'
    ];
    protected $appends = ["require_full"];

    public function post() {
        return $this->morphToMany('App\Models\UserPost', 'postable', 'id');
    }

    public function getRequireFullAttribute() {
        return $this->attributes;
    }

    public function postMedia() {
        return $this->hasMany('App\Models\PostMedia', 'user_post_id', 'id');
    }

    public function latestPostMedia() {
        return $this->hasOne('App\Models\PostMedia', 'user_post_id', 'id')->orderBy("id", "ASC");
    }

    public function postBoxes() {
        return $this->hasMany('App\Models\PostBox', 'user_posts_id', 'id');
    }

    public function place() {
        return $this->hasOne('App\Models\Location', 'id', 'location_id');
    }

    public function postLocation() {
        return $this->hasOne('App\Models\PostLocation', 'user_post_id', 'id');
    }

    public function postBoxesPivot() {
        return $this->belongsToMany('App\Models\Box', 'user_posts_boxes', 'user_posts_id', 'box_id');
    }

    public function postTagsPivot() {
        return $this->belongsToMany('App\Models\User', 'post_tag_users', 'user_post_id', 'user_id');
    }

    public function postTotalLikes() {
        return $this->hasOne('App\Models\Like', 'liked_to', 'id')
                        ->selectRaw('liked_to,count(*) as total_likes ')
                        ->groupBy('liked_to');
    }

    public function postTotalTags() {
        return $this->hasOne('App\Models\PostTagUser', 'user_post_id', 'id')
                        ->selectRaw('user_post_id,count(*) as total_tags ')
                        ->groupBy('user_post_id');
    }

    public function postTotalComments() {
        return $this->hasOne('App\Models\PostComment', 'user_post_id', 'id')->where(function($sql) {
//                            $sql->where('archive', '=', false);
                        })->selectRaw('user_post_id,count(*)  as total_comments ')
                        ->groupBy('user_post_id');
    }


}

从上面的模型中,我只想选择四个关系名称postMedia,place,postBoxes和location。 我不知道该如何实现此目标,即使我在Google搜索上找不到有用的解决方案也是如此。

0 个答案:

没有答案