This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

enricoeur
enricoeur

Hello,

because i created a profile author page on the front end i would like to list there all posts he did.

1) i create a CMS page with following slug:

/member/:id

Where id is containing the user id.

Inside this page i added a component PostList with alias "UsersPostList"

Inside the Partials so i started to edit template of the componet and inside CODE view i added the following code:

<?php

use RainLab\Blog\Models\Post;
use System\Models\File;

function onInit() {
    $id = $this->param('id');
    $this['stories'] = Db::table('rainlab_blog_posts')->where('author_id', $id)->first();
}

Now the point is that my array of post is always empty.

Whats should i do to fix ?

Last updated

daftspunky
daftspunky

This example is taken directly from the documentation:

url = "/blog"
==
use Acme\Blog\Classes\Post;

function onStart()
{
  $this['posts'] = Post::orderBy('created_at', 'desc')->get();
}
==
<h2>Latest posts</h2>
<ul>
    {% for post in posts %}
        <h3>{{ post.title }}</h3>
        {{ post.content }}
    {% endfor %}
</ul>

It is a working example. It would be a good idea to share your entire page template contents, found in the /themes directory of your application. This will give a clearer picture about what you're trying to do

enricoeur
enricoeur

1) my blog Plugin is Rainlab Blog

2) this is my pages list: and in particular as you can see i create a MEMBER PAGE /member/:id

3) what i want is to show user's information in the user's page so if user will visity /member/1 will see all details of this user. And inside his page will see list of all Blog Posts he published.

https://prnt.sc/qm5tpi

Now to do this as you see i tought to use "Posts List" components from Rainlab Blog Components.

And because it was requiring some advanced customization i give him a custom Alis Name. THe name is "UsersRacconti".

https://prnt.sc/qm5vam

What i want is to write a Custom CODE that will retrive in the list only Posts of the current user and not full list of all Posts from different users.

Thsi is my front end design:

https://prnt.sc/qm5vyh

In that list "Racconti di Webmaster" should appear only list of his Posts not Posts from other users.

That is all

daftspunky
daftspunky

I see a potential problem in your code, change this

{% set posts = __SELF__.posts %}

To this

{% set posts = UsersRacconti.posts %}

To clarify, __SELF__ is a variable that is only available within a component. When you use it in a partial, you must rename it to the component name

enricoeur
enricoeur

I understand but nothing....maiby it is queestion of my query

this is my table inside mysql

https://prnt.sc/qm6keb

So that means if everything was working fine i should be able in case i visit this url

/member/3

Show up on his Posts list that result, but list continue to be fully empty.

Thsi is my query

$this['posts'] = Db::table('rainlab_blog_posts')->where('author_id', $id)->first();

Any idea ?

Thanks

daftspunky
daftspunky

Everything looks fine here except you are calling ->first() which will only return a single result. If you want to return multiple posts you will want to use ->get() instead

$this['posts'] = Db::table('rainlab_blog_posts')->where('author_id', $id)->get();
enricoeur
enricoeur

Thanks...but it doesnt work in any way....

Now fro mwhat i see the query send back no any result...

because the list of posts is always 0'.

Or maybe for some reason that vector with result is ignored..

daftspunky
daftspunky

Try changing onInit to onEnd, maybe this is what happens

  • onInit - your posts are being set via the code block
  • onStart - the blogPost component is now setting posts, your posts are lost
  • onEnd - set your posts here instead
enricoeur
enricoeur

A bit better but now the list doesnt contain post infromations except the title,

Probably the componets was filling the posts array with some more complex query and my query https://prnt.sc/qm87rb

Is not the correct one to have a consistent list

Last updated

netsmertia
netsmertia

First use the debugbar plugin that will give you many information including all the queries. If you a getting all post by direct query then I don't think you need to use PostList Component. And may be only title is shown because of markup of PostList component.

enricoeur
enricoeur

No nothing.

I mean right now the query looks to be fully working because effectively i'm able to retrive list of posts of the current user.

This is the code: function onStart() { $id = $this->param('id'); $this['posts'] = Db::table('rainlab_blog_posts')->where('user_id', $id)->get(); }

If placed on Start / onEnd and so on is independet because it works always.

The issue now is that i'm unable to render on my view the elements of the post except the title.

This is strange because i'm using the same code i used for common list of posts ( i mean posts retrived using the normal query of the component.

I mean {{ post.title }} print the title, but{{ post.url }} or{{ post.summary }} and so on they dont print nothing but a blank space.

Where is the issue here ?

Last updated

enricoeur
enricoeur

ok i attach here full code of my application.

At first the code of the member page: /member/:id

MARKUP

<!-- content_block -->
<div class="content_block">
    <div class="container">
        <div class="row">

            <div class="col-md-4 sidebar">
                <div class="row">
                    <div class="col-xs-12 static_block">
                        <div class="side_edit_profile">
                            <div class="side_edit_profile_block">
                                <div class="side_edit_profile_img">
                                    <img src="{{ user.avatar | resize(200, 200, { mode: 'crop', quality: '80', extension: 'png' }) }}" alt="">
                                </div>
                                <div class="side_edit_profile_name">
                                    <span>{{ profile.name }}</span>
                                    <a href="#" class="icon-link-ext" rel="nofollow" target="_blank"></a>
                                </div>
                                <ul class="edit_profile_menu">
                                    <li><a href="#" class="icon-list">Annunci</a></li>
                                    <li><a href="#" class="icon-bell-alt">Notifiche</a></li>
                                    <li><a href="#" class="icon-user">Account</a></li>
                                    <li><a href="#" class="icon-heart">Osservati</a></li>
                                    <li><a href="#" class="icon-mail">Messaggi</a></li>
                                    <li><a href="#" class="icon-off">Esci dal sito</a></li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-xs-12">
                        <h3 class="block_title"><span class="icon-edit-alt"></span>Biografia di {{ profile.name }}</h3>
                    </div>
                    <div class="col-xs-12">
                        <div class="bordered_block">
                            <p>testo modificabile su pannello di controllo</p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-8">

                <div class="row profile_photo_block">
                    <div class="col-xs-12">
                        <h3 class="block_title"><span class="icon-list"></span>Racconti di {{ profile.name }}</h3>
                    </div>
                    <div class="col-xs-12">

                <div class="row racconti_list">
                    <div class="col-xs-12">
                        {% component 'UsersRacconti' %}
                    </div>
                </div>

            </div>

        </div>
    </div>
</div>

CODE

use RainLab\User\Models\User;
use System\Models\File;

function onInit() {
    $id = $this->param('id');
    $this['profile'] = Db::table('users')->where('id', $id)->first();
}

Inside the partial /UsersRacconti/default

This is my

MARKUP

{% set racconti = posts %}

<div class="row racconti_list">
                    <div class="col-xs-12">
                        <div class="racconti_blocks">

    {% for post in racconti %}
                            <div class="racconti_block">
                                <div class="racconti_block_img">
                                {% for image in post.featured_images|slice(0,1) %}
                                    <a href="{{ post.url }}"><img  data-src="{{ image.filename }}" src="{{ image.path | resize(104, 104, { mode: 'crop', quality: '80', extension: 'png' }) }}" alt="{{ image.description != null ? image.description : image.filename }}" title="{{ image.title != null ? image.title : post.title }}"></a>
                                {% endfor %}
                                <span class="gender icon-venus"></span>
                                </div>
                                <div class="racconti_block_content">
                                    <h3 class="racconti_block_title"><a href="{{ post.url }}">{{ post.title }} {% if post.published== 0 %} [BOZZA] {% endif %}</a></h3>
                                    <div class="racconti_block_text">
                                        <p>{{ post.summary }}</p>
                                    </div>
                                    <p class="racconti_block_info"><span>Genova</span>, {{ post.published_at.diffForHumans }}</p>
                                </div>
                                <div class="racconti_block_contact">
                                    <a href="{{ post.url }}" class="racconti_contact_btn"><span class="icon-mobile"></span>Contatta utente</a>
                                </div>
                            </div>
    {% else %}
        <p>{{ __SELF__.noPostsMessage }}</p>
    {% endfor %}
                        </div>
                    </div>
                </div>

</ul>

{% if posts.lastPage > 1 %}
                <div class="row">
                    <div class="col-xs-12">
                        <ul class="pagination_block">
        {% if posts.currentPage > 1 %}
            <li><a href="{{ this.page.baseFileName|page({ (__SELF__.pageParam): (posts.currentPage-1) }) }}"><span class="icon-angle-left"></span></a></li>
        {% endif %}

        {% for page in 1..posts.lastPage %}
            <li class="{{ posts.currentPage == page ? 'current_page' : null }}">
                <a href="{{ this.page.baseFileName|page({ (__SELF__.pageParam): page }) }}">{{ page }}</a>
            </li>
        {% endfor %}

        {% if posts.lastPage > posts.currentPage %}
            <li><a href="{{ this.page.baseFileName|page({ (__SELF__.pageParam): (posts.currentPage+1) }) }}"><li><a href="#"><span class="icon-angle-right"></span></a></li></a></li>
        {% endif %}
                        </ul>
                    </div>
                </div>
{% endif %}

inside CODE view


<?php 

use RainLab\Blog\Models\Post;

use System\Models\File;

function onStart() {
    $id = $this->param('id');
    $this['posts'] = Db::table('rainlab_blog_posts')->where('user_id', $id)->get();
}

It is all

enricoeur
gbonamie52886
gbonamie52886

FYI, in the first section of the UsersRacconti component you are missing an opening <ul>.

enricoeur
enricoeur

If the only one way to have support is to pay that 96 USD please tell me i will pay for them,. Butr i need to be sure iccan ask things like this paying that plan...

enricoeur
enricoeur

Niuce platform i like...butr really slow support. No any user able to point me in the right direction with this ?

Never had to wait so many days i na forum to have a suggestion...

enricoeur
enricoeur

[SOLVED]

$this['posts'] = RainLab\Blog\Models\Post::where('user_id',$id)->get();

Really i dont understand why was so difficult for the mdoerator ( that as i see is also plugin author of Rainlab ) to give me this simple string.

Thanks

Last updated

gbonamie52886
gbonamie52886

@enricoeur I am glad you found the solution -I was going to look into it either today or tomorrow, but you beat me to it :) Also thanks for posting your solution, which I am sure will help people in the future.

1-18 of 18

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.