Laravel - 如何使用刀片模板中的foreach循环显示集合

时间:2016-04-17 22:07:47

标签: php laravel eloquent push blade

我有这段代码:

public function userbase($id)
{
    $articles = Auth::user()->articles()->get();
$offers = collect([]);
    foreach ($articles as $article) {
        $offers->merge(Offer::where('article_id',  $article['id'])->groupBy('user_id')->get());
    }
//return $offers;
    return view('articles.userbase', compact('article','offers'));
 }

在查看刀片我有:

 @foreach ($offers as $offer)
            <tr>
                <td>{{$offer->user->name}}</td>
                <td>{{$offer->user->email}}</td>
                <td>{{$offer->user->phone}}</td>
                <td class="col-md-3 text-right"><a href="#" class="btn btn-success">Send message</a></td>
            </tr>
            @endforeach

但是$ offer会给我这个结果[],所以我不能把它显示给刀片......

我也试试:

$offers->push(Offer::where('article_id',  $article['id'])
       ->groupBy('user_id')
       ->get());

然后我得到:

[
    [
        {
            "id": 101,
            "user_id": 8,
            "article_id": 8,
            "price": 55,
            "comment": "",
            "created_at": "2016-04-12 16:24:21",
            "updated_at": "2016-04-12 16:24:21",
            "key": "",
            "title": "",
            "start": "Wed, 20 Apr 2016 00:00:00 +0000",
            "provera": 4
        },
        {
            "id": 180,
            "user_id": 11,
            "article_id": 8,
            "price": 55,
            "comment": "",
            "created_at": "2016-04-17 15:12:53",
            "updated_at": "2016-04-17 15:12:53",
            "key": "",
            "title": "",
            "start": "Wed, 01 Jun 2016 00:00:00 +0000",
            "provera": 41
        }
    ],
    [
        {
            "id": 132,
            "user_id": 8,
            "article_id": 13,
            "price": 11,
            "comment": "",
            "created_at": "2016-04-13 20:05:06",
            "updated_at": "2016-04-13 20:05:06",
            "key": "",
            "title": "",
            "start": "Tue, 19 Apr 2016 00:00:00 +0000",
            "provera": -1
        }
    ],
    [
        {
            "id": 175,
            "user_id": 8,
            "article_id": 14,
            "price": 22,
            "comment": "",
            "created_at": "2016-04-16 10:06:51",
            "updated_at": "2016-04-16 10:06:51",
            "key": "",
            "title": "",
            "start": "Tue, 03 May 2016 00:00:00 +0000",
            "provera": 11
        }
    ],
    [],
    []
]

我再也不能向刀片模板展示了... 另外,我需要有唯一的user_id,所以我需要获得uniqe id的第一个优惠...

我怎么能让它成为可能?

0 个答案:

没有答案