How I use listivew when using related data?

I am wanting to display posts that a user has liked using the listview widget therefore the dataprovider must contain the correct records as I dont want to be doing any query in my view.

I can access post data using the following in my view:


<?php foreach ($likedposts as $post) { ?>

    <?php var_dump($post->likedpost['title']); ?>

<?php } ?>

My controller is as follows:

I am wanting to display posts that a user has liked using the listview widget therefore the dataprovider must contain the correct records as I dont want to be doing any query in my view.

I can access post data using the following in my view:

<?php foreach ($likedposts as $post) { ?>

&lt;?php var_dump(&#036;post-&gt;likedpost['title']); ?&gt;

<?php } ?>

My controller is as follows:


public function actionIndex()

{


    $user_id = Yii::$app->user->id; 


    $likedposts =  Likedposts::find()->where([ 'user_id' => $user_id])->all();


    return $this->render('index', [

        'likedposts' => $likedposts,

    ]);

}

I want to be able to access data for example like below:


<?php foreach ($likedposts as $model) { ?>

    <?php var_dump($model->title); ?>

<?php } ?>

Schema Users

-id

-name

-email

-password

Blog Post

-id

-title

-content

Liked Posts

-id

-post_id

-user_id

The below the first variable I need to get all the liked posts however I only really need to select the post_id from the table as it will be used in the dataprovider which will then get all posts which id fall within the likedpost variable(need to create an array of variables maybe?)


  $likedposts =  Likedposts::find()->where([ 'user_id' => $user_id])->all();

    $dataProvider = Posts::find()->where(['id'] => $likedposts->post_id])->all();

[color="#FF0000"]Before replying to the current post, I would like to ask you as a moderator.

Are your previous issues all sorted out?

Please give feedback to the replies given by our community members who wanted to help you for free.

[/color]

Have you already set up relations between User and Post?