samedi 21 septembre 2019

How to get latest post from threads with several fields?

In my Laravel 5.8/mysql5 app I get latest post from threads like

$forumThreads= ForumThread
    ::getByForumId($forum_id)
    ->select(
        'forum_threads.*',
        \DB::raw(' ( select count('.$prefix.'forum_posts.id) from '.$prefix.'forum_posts where '.$prefix.'forum_posts.forum_thread_id = '.$prefix.'forum_threads.id ) as forum_posts_count'),
        \DB::raw(' ( select created_at from '.$prefix.'forum_posts where '.$prefix.'forum_posts.forum_thread_id = '.$prefix.'forum_threads.id  order by id desc limit 1) as latest_post_created_at')
    )
    ->orderBy($order_by, $order_direction)
    ->offset($limit_start)
    ->take($forum_threads_per_page)
    ->get();

It works ok, but I need to get several fields for latest post. If I change condition as :

\DB::raw(' ( select created_at, title, slug from '.$prefix.'forum_posts where 

I got error :

Cardinality violation: 1241 Operand should contain 1 column(s)

Of there is a way ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire