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) { ?>
<?php var_dump($post->likedpost['title']); ?>
<?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
-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();