如何通过Laravel获取10月CMS中帖子的精选图片?

时间:2017-06-28 17:45:28

标签: php laravel blogs octobercms

我正在为我的博客使用RainLab.Blog插件。此外,我正在尝试为我的网站创建订阅脚本,并成功设法从数据库中获取titleslug参数,每当我想通过邮件发送最新帖子时使用它们。一切正常,但我也想发送精选图片,可以在创建时添加到帖子中:img

有没有办法将它包含在下面的脚本中,考虑到我已经从rainlab_blog_posts表中提取了title和slug参数。显然,此表中没有指向特色图像的链接:

foreach($blogPosts as $posts) 
{
$text .= '<a href="https://somesite.com/blog/post/'.$posts->slug.'">'.$posts->title.'</a><br>'; 
 }

1 个答案:

答案 0 :(得分:3)

您应该检查帖子模型以查找帖子和图片之间的关系

如果方法名称为 featured_images ,那么您可以使用$posts->featured_images

获取图片

但我猜 featured_images 不是一对一的关系,每个帖子可能有多个图片

因此您应该决定要显示哪些图像

示例代码中的

foreach($blogPosts as $posts) 
{
$text .= '<a href="https://somesite.com/blog/post/'.$posts->slug.'">'.$posts->title.'</a><br>';

$image .= $posts->featured_images[0]->path; 
}

featured_images [0]获取featured_images中的第一张图片,path也是存储特色图片路径的列名,以便在img src中添加此路径

$posts->featured_images->first()->path是获取帖子

图像的另一种方法