Ad

How Can I Get Featured Image For A Post In October CMS Via Laravel?

- 1 answer

I'm using RainLab.Blog plugin for my blog. Also I'm trying to create a subscription script for my site and successfully managed to get both title and slug parameters from the database,use them whenever I want to send latest posts via mail. Everything works fine, but I also want to send featured image, which can be added to the post, while it's being created : img

Is there a way to include it in the following script, considering that I've already extracted both title and slug parameters from rainlab_blog_posts table. And obviously there are no links to featured images in this table :

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

Answer

You should check Post Model to find relation between posts and images

if method name is featured_images so you can get images with $posts->featured_images

But I guess featured_images is not one to one relation and each post could have more than one image

so you should decide which images you want to get to display

in your sample code:

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

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

featured_images[0] get first image in featured_images and also path is the column name that store path of featured image to add this path in img src

also

$posts->featured_images->first()->path is another way to get an image of post

Ad
source: stackoverflow.com
Ad