簡體   English   中英

Laravel 5緊湊地訪問數據

[英]Laravel 5 accessing data with compact


大家好,

我希望我的帖子會有意義,並且您不會認為我是新手(有時我是..)

我試圖了解什么是將數據發送到laravel 5中的視圖並訪問這些數據的最佳方法。

我實際上可以做到,但是在我看來這是錯誤的方法。

這是我的控制器:

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'));
}

如您在開始時所看到的,我創建了一個名為$ array_articles的數組,然后將行插入其中。

在將其發送到視圖之前,我先使用JSON對其進行編碼,然后在視圖中執行JSON解碼。

我的觀點:

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

如果我不執行此JSON編碼和解碼,則無法訪問我的數據。

那我做對了嗎?

@更新

如果我不使用JSON進行編碼和解碼,那么我總是得到:嘗試訪問非對象的屬性,無論如何將其傳遞給視圖,也不管如何調用數組。

這是我的$ article_array的print_r:

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 

您不需要編碼和解碼,因為這只會浪費時間。 您可以將PHP變量提供給您的視圖,無需任何編碼:

return view('timeline', array('articles' => (object) $array_articles));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM