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

stealhex's avatar

Should I use API Resources or Eluquent query for joinin tables

Hi guys I'm new here. I want to ask question. I have 3 tables on database, these are posts, post_views and votes. For every post row on posts table have multiple view and votes row. I split them to another tables because I want to hold voters and viewers ip addresses too. Lastly my votes table has a column which is holding vote type as integer. However, I wrote eloquent ORM query, that have 4 sub joins for fetch each count of vote for every type on and view count for every post. I forgot the say that, I also write relationship functions to every model(one to many). As you know API resources has a proxy for models. I think that, I could use this proxy for each count in resource and I did it. I observed that, if I use proxy method for listing posts, that consuming much more time for response than sub Joins. I want to ask which approach better for this situation? Should I use resources or eloquent for table joining ?

If you wonder that my post model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title',
        'description',
        'image',
        'mdFile',
        'readCount',
        'keys',
        'lang',
        'published'
    ];

    /**
     * Get the views for the blog post.
     */
    public function views()
    {
        return $this->hasMany('App\Models\PostView');
    }

    /**
     * Get the views for the blog post.
     */
    public function votes()
    {
        return $this->hasMany('App\Models\Vote');
    }
}

and thats my eloquent query

$thumbsUps = Vote::select('post_id', Vote::raw('COUNT(id)'))
    ->where('vote_type', 1)
    ->groupBy('post_id');

$thumbsDowns = Vote::select('post_id', Vote::raw('COUNT(id)'))
    ->where('vote_type', 2)
    ->groupBy('post_id');

$hearts = Vote::select('post_id', Vote::raw('COUNT(id)'))
    ->where('vote_type', 3)
    ->groupBy('post_id');

if($request->query('search')){
    $search = $request->query('search');
    return PostResource::collection(Post::where($filters)
        ->leftJoin('post_views', 'posts.id', '=', 'post_views.post_id')
        ->selectRaw('
            posts.*,
            COUNT(post_views.id) as view_count,
            COUNT(thumbs_ups) as thumbs_ups,
            COUNT(thumbs_downs) as thumbs_downs,
            COUNT(hearts) as hearts')
        ->leftJoinSub($thumbsUps, 'thumbs_ups', function ($join) {
            $join->on('posts.id', '=', 'thumbs_ups.post_id');
        })
        ->leftJoinSub($thumbsDowns, 'thumbs_downs', function ($join) {
            $join->on('posts.id', '=', 'thumbs_downs.post_id');
        })
        ->leftJoinSub($hearts, 'hearts', function ($join) {
            $join->on('posts.id', '=', 'hearts.post_id');
        })
        ->groupBy('posts.id')
        ->orderBy($orderColumn, $orderBy)
        ->paginate(12)
    );
}
0 likes
0 replies

Please or to participate in this conversation.