В моем приложении Laravel 5.8 / mysql5 я получаю последние сообщения из тем вроде
$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();
Все работает нормально, но мне нужно получить несколько полей для последней записи.Если я изменяю условие следующим образом:
\DB::raw(' ( select created_at, title, slug from '.$prefix.'forum_posts where
Я получаю ошибку:
Cardinality violation: 1241 Operand should contain 1 column(s)
Есть способ?
модифицированный блок: Вapp / ForumThread.php Я создал
public function latestForumPost()
{
return $this->hasOne('App\ForumPost')->latest();
}
public function forumPosts()
{
return $this->hasMany('App\ForumPost');
}
и под моим контролем я делаю:
$forumThreads= ForumThread::with('latestForumPost')->withCount('forumPosts')
->getByForumId($forum_id)
->orderBy($order_by, $order_direction)
->offset($limit_start)
->take($forum_threads_per_page)
->get();
foreach( $forumThreads as $next_key=>$nextForumThread ) {
$nextForumThread->latest_post_created_at= $nextForumThread->latestForumPost->created_at;
}
и это работает!Но когда я пытаюсь использовать $ nextForumThread-> latestForumPost в моем файле blade.php:
<tr v-for="nextForumThread, index in forumThreads" :key="nextForumThread.id">
nextForumThread.latestForumPost::{{ nextForumThread.latestForumPost}}
Это не работает.Можно ли использовать в шаблоне лезвия?