Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

beyond's avatar

Laravel Query

Please I need with laravel complex query.

I have post table, post files table, post comments table, user table, post like monitor table, post files like monitor table.

i want to fetch post with files and comments and check if the user at the front end has already liked the post or each file associated with a particular post.

i could find my way with post and check if already liked, files, comments with commenter details, but it remains to check each files if already liked?

Here is the code so far:

return StatusPost::with([ // get poster details 'poster:id,userName,profilePic',

                // get associated files
/* This is the part I need help with by getting the row if the each file has been liked by the user from the post_file_like_monitors table*/
                'status_files',

                // get the row if the post has been liked by participant
                'post_liked'=>function($query){ 
                    $query->where('user_id', 1);
                },

                'comments'=>function($query){   //get the comment and commenters details
                    $query->join('users', 'user_id', '=','users.id')
                    ->select('status_post_comments.*', 'users.userName', 'users.profilePic');
                },
            ])
            ->whereIn('user_id', // Query to get the followings post 
                function($query){
                    $query->select('following_id')
                    ->from('followers')
                    ->where('followee_id', 2);
                }
            )
            ->orWhere('user_id', 3) // or the self post
            ->orderBy('created_at', 'DESC') // in latest mode
            ->get();

Below is the post Model:

class StatusPost extends Model {

public function poster(){ //get the poster details
    return $this->belongsTo('App\User', 'use_id', 'id');
}

public function status_files(){
    return $this->hasMany('App\StatusFile');
}

public function comments(){
    return $this->hasMany('App\StatusPostComment')->take(6);
}

public function post_liked(){
    return $this->hasOne('App\StatusPostLikeMonitor');
}

}

0 likes
2 replies
beyond's avatar

I want to achieve a tree like this:

Data:{ Post details, liked, // this checks the post like history table files:[ file one:{ details, liked // this should also check the files like history table but unable to achieve this for now and need help with it }, file two:{ details, }, etc... ], comments:[ comment one:{ details, commenter }, comment two:{ details, commenter }, etc... ] }

beyond's avatar

{ "id": 14, "description": "I am so happy today, Now let me post Pictures", "likes": 1, "created_at": "2020-03-19 23:28:15", "poster": { "userName": "name", "profilePic": "pic.jpg" }, "status_files": [ { "id": 37, "name": "5297.jpg", "status_post_id": 14, "likes": 1, "created_at": "2020-03-19 23:28:15" // I want to check if the current user was the one that liked the file. }, { "id": 38, "name": "5450.jpg", "status_post_id": 14, "likes": 0, "created_at": "2020-03-19 23:28:15" }, { "id": 39, "name": "537.jpg", "status_post_id": 14, "likes": 0, "created_at": "2020-03-19 23:28:15" }, { "id": 40, "name": "696.jpg", "status_post_id": 14, "likes": 0, "created_at": "2020-03-19 23:28:15" } ], "post_liked": null, "comments": [ { "id": 61, "status_post_id": 14, "comment": "Number 14, Comment on the status to see if it works", "created_at": "2020-03-21 14:05:45", "userName": "name", "profilePic": "profilePictures31.jpg" }, { "id": 62, "status_post_id": 14, "comment": "Number 14, Comment on the status to see if it works", "created_at": "2020-03-21 14:26:56", "userName": "beyond", "profilePic": "profilePictures34.jpg" }, { "id": 63, "status_post_id": 14, "comment": "Number 14, Comment on the status to see if it works", "created_at": "2020-03-21 14:27:04", "userName": "Mrpro", "profilePic": null },

    ]
},

Please or to participate in this conversation.