Hi everybody,
I hope my post will make sense and you won't think I am total newbie (which I am sometimes..)
I am trying to understand what's the best way to send data to a view in laravel 5 and access these data.
I actually can do it but it feels to me that's a wrong way to do it.
Here is my controller:
public function timeline()
{
$array_articles = array();
$sharedcontents = DB::table('follow')->select(DB::raw('GROUP_CONCAT(sharedcontent_id) as concat_article_id'))
->join('sharedcontent_user', 'sharedcontent_user.user_id', '=', 'follow.user_id')
->where('follower_user_id', Auth::user()->id)
->groupBy('sharedcontent_user.sharedcontent_id')
->get();
foreach ($sharedcontents as $sharedcontent) {
$article_ids = $sharedcontent->concat_article_id;
$shareNumber = DB::table('sharedcontent_user')->where('sharedcontent_id', $article_ids)->count();
$row['share_number'] = $shareNumber;
$articles = DB::table('shared_content')
->select( 'sharedcontent_user.sharedcontent_id as sharedId',
'shared_content.id as articleId',
'shared_content.title as articleTitle',
'shared_content.source as articleSource',
'shared_content.image as articleImage'
)
->join('sharedcontent_user', 'sharedcontent_user.sharedcontent_id', '=', 'shared_content.id')
->join('users', 'users.id', '=', 'sharedcontent_user.user_id')
->get();
foreach ($articles as $article){
$shared_content_id = $article->articleId;
$row['title'] = $article->articleTitle;
$row['source'] = $article->articleSource;
$row['image'] = $article->articleImage;
}
array_push($array_articles, $row);
}
$articles = json_encode($array_articles);
return view('timeline', compact('articles'));
}
As you can see at the beginning I create an array called: $array_articles and I then push my row into it.
Before sending it to my view I encode it with JSON and in my view I perform a JSON decode.
My view:
<div id="timeline-container" class="container">
<?php $articles = json_decode($articles); ?>
@foreach ($articles as $article)
<div id="article-shared-box">
<div class="article-shared-box-left pull-left">
<div class="blend-layer">
<div class="article-shared-picture"><img src="{{ url($article->image) }}" /></div>
<div class="category-square">{{ $article->category }}</div>
<div class="article-shared-title">{{ $article->title }}</div>
<div class="article-shared-source">{{ $article->source }}</div>
</div>
...
If I don't perform this JSON encode and decode I cannot access my data.
So am I doing it right ?
@update
If I don't encode and decode with JSON I always get the: try to access the property of a non object, no matter how I pass it to the view and no matter how I call my array.
Here is a print_r of my $article_array:
Array ( [0] => Array ( [share_number] => 1 [category] => Startup [sharing_user_id0] => 68 [sharing_user_name0] => 건우 박 [sharing_user_picture0] => https://graph.facebook.com/v2.4/723882664388388/picture?type=normal [comment_number] => 1 [most_recent_comment] => hhhh [comment_author] => 건우 박 [title] => 전북도 내 스타트업 육성 … 300억 규모 창조경제 혁신펀드 본격 운용 [source] => Platum [image] => http://platum.kr/wp-content/uploads/2015/07/11941_800799769932697_1056826482061661861_n.jpg ) [1] => Array ( [share_number] => 1 [category] => Startup [sharing_user_id0] => 68
You don't need to encode and decode as this just wastes time. You can give PHP variables to your view, there's no need for any encoding:
return view('timeline', array('articles' => (object) $array_articles));