如何通过 Laravel 在 October CMS 中获取 post 的精选图片?
How can I get featured image for a post in October CMS via Laravel?
我正在为我的博客使用 RainLab.Blog 插件。此外,我正在尝试为我的网站创建一个订阅脚本,并成功地从数据库中获取了 title
和 slug
参数,只要我想通过邮件。一切正常,但我还想发送特色图片,可以在创建时将其添加到 post:
考虑到我已经从 rainlab_blog_posts
table 中提取了 title 和 slug 参数,有没有办法将它包含在以下脚本中。显然,此 table 中没有指向特色图片的链接:
foreach($blogPosts as $posts)
{
$text .= '<a href="https://somesite.com/blog/post/'.$posts->slug.'">'.$posts->title.'</a><br>';
}
您应该检查 Post 模型以找到 post 和图像
之间的关系
如果方法名称是 featured_images 那么您可以使用 $posts->featured_images
获取图像
但我想 featured_images 不是一对一的关系,每个 post 可能有不止一张图像
因此您应该决定要显示哪些图像
在您的示例代码中:
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 中获取第一张图片,同时路径是存储特色图片路径的列名称,以便将此路径添加到 img src
还有
$posts->featured_images->first()->path
是获取 post
图像的另一种方法
我正在为我的博客使用 RainLab.Blog 插件。此外,我正在尝试为我的网站创建一个订阅脚本,并成功地从数据库中获取了 title
和 slug
参数,只要我想通过邮件。一切正常,但我还想发送特色图片,可以在创建时将其添加到 post:
考虑到我已经从 rainlab_blog_posts
table 中提取了 title 和 slug 参数,有没有办法将它包含在以下脚本中。显然,此 table 中没有指向特色图片的链接:
foreach($blogPosts as $posts)
{
$text .= '<a href="https://somesite.com/blog/post/'.$posts->slug.'">'.$posts->title.'</a><br>';
}
您应该检查 Post 模型以找到 post 和图像
之间的关系如果方法名称是 featured_images 那么您可以使用 $posts->featured_images
但我想 featured_images 不是一对一的关系,每个 post 可能有不止一张图像
因此您应该决定要显示哪些图像
在您的示例代码中:
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 中获取第一张图片,同时路径是存储特色图片路径的列名称,以便将此路径添加到 img src
还有
$posts->featured_images->first()->path
是获取 post