Search code examples
phplaravelblogsoctobercms

How can I get featured image for a post in October CMS via Laravel?


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 href="https://somesite.com/blog/post/'.$posts->slug.'">'.$posts->title.'</a><br>'; 
 }

Solution

  • 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 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