Laravel 5加从查询结果在foreach数组 [英] Laravel 5 add results from query in a foreach to array

查看:2933
本文介绍了Laravel 5加从查询结果在foreach数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel 5应用程序有问题。
我的控制器方法中包含一个foreach,这应该遍历previous雄辩,结果阵列(它被命名为$拉链codeS)。因此,每个拉链code是用来填补查询,导致回报率的文章对象(命名为$文章)。
迭代的每一个到底应该的文章,结果添加到一个数组($文章),它被用来在我的方法的末尾,以显示我的网页上的文章。
也许没有为文章查询,并不是每个迭代的执行更好的选择,但我不知道怎么办。

更新

这是工作了我的问题,没有在foreach执行查询:<一href=\"http://stackoverflow.com/questions/33719951/laravel-multiple-where-clauses-in-query-from-given-array\">Laravel查询多个where子句从给定数组

我的code现在是

  //抓住所有拉链codeS匹配的距离
$拉链codeS = $这个 - &GT; getZip codeS($ zipCoordinateId,$距离);的foreach($拉链codeS AS $关键=&GT; $ VAL)
{
    $拉链codeS [$关键] =(阵列)$ VAL;
}$codeS = array_column($拉链codeS,'zc_zip');$ =篇文章::其中('拉链code',$codeS) - GT;获得();返回视图(pages.intern.articles.index',紧凑的('文章'));

更新结束

我SearchController:

 &LT; PHP空间应用\\ HTTP \\控制器;使用应用程序\\文章;
使用照亮\\ HTTP \\请求;
使用照亮的\\ Support \\外立面\\ DB;
使用照亮的\\ Support \\外立面\\会议;类SearchController扩展控制器
{
公共职能getSearch(请求$要求)
{
    如果(($请求 - &GT;拥有('拉链code'))){
        $拉链code = $请求 - &GT;输入('拉链code');
        $距离= $请求 - &GT;输入(距离);        $ zipCoordinateId = $这个 - &GT; getZipCoordindateId($拉链code);
        $拉链codeS = $这个 - &GT; getZip codeS($ zipCoordinateId,$距离);        $文章=阵列();        的foreach($拉链codeS为$值){
            // $拉链code = $拉链codeS [0] - &GT; zc_zip; //这是工作
            $拉链code = $值 - &GT; zc_zip;            $文章= ::条的地方('拉链code',$拉链code) - &GT;获得();
           //每次迭代添加$文章$文章
        }        返回视图(pages.articles',紧凑的('文章'));
    }其他{
        返回重定向('/篇);
    }
}公共职能getZipCoordindateId($值){
    $结果= DB ::表('zip_coordinates')
             - 化合物其中('zc_zip','=',$值)
             - &GT; orWhere('zc_location_name','LIKE',$值)
             - &GT;采摘('zc_id');
    返回$结果;
}公共职能getZip codeS($ ID,$距离){    $结果= DB ::选择(
        DB:原料(
            SELECT dest.zc_zip,
                ACOS(
                    SIN(RADIANS(src.zc_lat))* SIN(RADIANS(dest.zc_lat))
                    + COS(RADIANS(src.zc_lat))* COS(RADIANS(dest.zc_lat))
                    * COS(RADIANS(src.zc_lon) - RADIANS(dest.zc_lon))
                )* 6380 AS距离
            从zip_coordinates AS DEST
            CROSS JOIN zip_coordinates AS SRC
            WHERE src.zc_id =:ID
            HAVING距离&LT; :距离
            ORDER BY距离),阵列('ID'=&GT;的$ id,距离= GT; $距离));    返回$结果;
}
}

我的观点:

  @extends('layouts.default')@section(内容)
    &LT; H1&GT;所有文章和LT; / H1&GT;
&LT;小时/&GT;&LT;搜索和GT;
    {!表::打开(['URL'=&GT;'实习生/搜索'])}!
        {!表::文本('拉链code',空,['类'=&GT;的形式控制'])}!
        {!表::选择(距离,[
          '5'= GT; '5知识网,
          '10'= GT; '10公里',
          '25'=&GT; '25公里',
          '50'=&GT; '50公里',
          ],空,['类'=&GT; 的形式控制'])}!
        {!表::提交(素贞',['类'=&GT;'BTN BTN-成功'])}!
    {!表:: close()方法!}
&LT; /搜索&GT;
@foreach(如$ $条文章)
    &lt;物品&GT;
        &LT; H2&GT;
            &LT; A HREF ={{URL('/篇',$物品─&GT; ID)}}&GT; {{$物品─&GT;名}}&LT; / A&GT;
        &LT; / H&GT;        &LT; D​​IV&GT;
            &LT;标签&gt;简介:LT; /标签&gt;
            &LT; P&GT; {{$物品─&GT;说明}}&LT; / P&GT;
        &LT; / DIV&GT;
        &LT; D​​IV&GT;
            &LT;标签&gt; ZIP code:&LT; /标签&gt;
            {{$物品─&GT;拉链code}}
        &LT; / DIV&GT;
        &LT; D​​IV&GT;
            &LT;标签&gt;发布时间:LT; /标签&gt;
            {{$物品─&GT;公布}}
        &LT; / DIV&GT;
        &LT; D​​IV&GT;
            &LT;标签&gt;等级:LT; /标签&gt;
            {{$物品─&GT;排名}}
        &LT; / DIV&GT;
    &LT; /条&GT;
    &LT;小时/&GT;
@endforeach
@停止

希望你能帮助我,谢谢提前。
quantatheist

修改

当我使用

 的foreach($拉链codeS为$关键=&GT; $值)
        {
            $文章[] =文章::在哪里('拉链code',$值 - &GT; zc_zip) - GT;获得();
        }
        的print_r($文章);

在寻求其他压缩code和更新拉链$ C $我的文章CS,它打印(目前,也有我的数据库三篇文章中,双方首先必须拉链codeS,这在我的新的zip code-搜索范围):

 阵列

[0] =&GT;照亮\\数据库\\锋\\集合对象
    (
        [项目:保护] =&GT;排列
            (
                [0] =&GT;应用程序\\对象条
                    (
                        [可填写:保护] =&GT;排列
                            (
                                [0] =&GT;名称
                                [1] =&GT;描述
                                [2] =&GT;轮廓
                                [3] =&GT; driverlicense
                                [4] =&GT;拉链code
                                [5] =&GT;发表
                                [6] =&GT;秩
                                [7] =&GT;联系
                                [8] =&GT;注意
                                [9] =&GT;图片
                                [10] =&GT;出版者
                            )                        [连接:保护] =&GT;
                        [表:保护] =&GT;
                        [PrimaryKey的:保护] =&GT; ID
                        [perPage:保护] =&GT; 15
                        [递增] =&GT; 1
                        [时间戳] =&GT; 1
                        [属性:保护] =&GT;排列
                            (
                                [ID] =&GT;五
                                [名] =&GT;测试1
                                [说明] =&GT; Lorem存有悲坐阿梅德,consetetur
                                [资料] =&GT; Lorem存有悲坐阿梅德,consetetur
                                [driverlicense] =&GT;是
                                [出版] =&GT;没有
                                [等级] =&GT; 10
                                [联系方式] =&GT; 1
                                [图片] =&GT;
                                [邮编code =&GT; 89429
                                【注意事项】= GT;
                                [发行] =&GT; 1
                                [created_at] =&GT; 2015年11月4日1点45分18秒
                                [的updated_at] =&GT; 2015年11月4日10时07分24秒
                            )                        [原创:保护] =&GT;排列
                            (
                                [ID] =&GT;五
                                [名] =&GT;测试1
                                [说明] =&GT; Lorem存有悲坐阿梅德,consetetur
                                [资料] =&GT; Lorem存有悲坐阿梅德,consetetur
                                [driverlicense] =&GT;是
                                [出版] =&GT;没有
                                [等级] =&GT; 10
                                [联系方式] =&GT; 1
                                [图片] =&GT;
                                [邮编code =&GT; 89429
                                【注意事项】= GT;
                                [发行] =&GT; 1
                                [created_at] =&GT; 2015年11月4日1点45分18秒
                                [的updated_at] =&GT; 2015年11月4日10时07分24秒
                            )                        [关系:保护] =&GT;排列
                            (
                            )                        [隐藏:保护] =&GT;排列
                            (
                            )                        [可见:保护] =&GT;排列
                            (
                            )                        [追加:保护] =&GT;排列
                            (
                            )                        [守卫:保护] =&GT;排列
                            (
                                [0] =&GT; *
                            )                        [日期:保护] =&GT;排列
                            (
                            )                        [日期格式:保护] =&GT;
                        [蒙上:保护] =&GT;排列
                            (
                            )                        [触摸:保护] =&GT;排列
                            (
                            )                        [观测:保护] =&GT;排列
                            (
                            )                        [有:保护] =&GT;排列
                            (
                            )                        [morphClass:保护] =&GT;
                        [存在] =&GT; 1
                        [wasRecentlyCreated] =&GT;
                    )            )    )[1] =&GT;照亮\\数据库\\锋\\集合对象
    (
        [项目:保护] =&GT;排列
            (
                [0] =&GT;应用程序\\对象条
                    (
                        [可填写:保护] =&GT;排列
                            (
                                [0] =&GT;名称
                                [1] =&GT;描述
                                [2] =&GT;轮廓
                                [3] =&GT; driverlicense
                                [4] =&GT;拉链code
                                [5] =&GT;发表
                                [6] =&GT;秩
                                [7] =&GT;联系
                                [8] =&GT;注意
                                [9] =&GT;图片
                                [10] =&GT;出版者
                            )                        [连接:保护] =&GT;
                        [表:保护] =&GT;
                        [PrimaryKey的:保护] =&GT; ID
                        [perPage:保护] =&GT; 15
                        [递增] =&GT; 1
                        [时间戳] =&GT; 1
                        [属性:保护] =&GT;排列
                            (
                                [ID] =&GT; 6
                                [名] =&GT; TEST2
                                [说明] =&GT; Lorem存有悲坐阿梅德,consetetur
                                [资料] =&GT; Lorem存有悲坐阿梅德
                                [driverlicense] =&GT;是
                                [出版] =&GT;没有
                                [等级] =&GT; 3
                                [联系方式] =&GT; 1
                                [图片] =&GT;
                                [邮编code =&GT; 89428
                                【注意事项】= GT;
                                [发行] =&GT; 1
                                [created_at] =&GT; 2015年11月4日1时45分38秒
                                [的updated_at] =&GT; 2015年11月4日9时58分01秒
                            )                        [原创:保护] =&GT;排列
                            (
                                [ID] =&GT; 6
                                [名] =&GT; TEST2
                                [说明] =&GT; Lorem存有悲坐阿梅德,consetetur
                                [资料] =&GT; Lorem存有悲坐阿梅德
                                [driverlicense] =&GT;是
                                [出版] =&GT;没有
                                [等级] =&GT; 3
                                [联系方式] =&GT; 1
                                [图片] =&GT;
                                [邮编code =&GT; 89428
                                【注意事项】= GT;
                                [发行] =&GT; 1
                                [created_at] =&GT; 2015年11月4日1时45分38秒
                                [的updated_at] =&GT; 2015年11月4日9时58分01秒
                            )                        [关系:保护] =&GT;排列
                            (
                            )                        [隐藏:保护] =&GT;排列
                            (
                            )                        [可见:保护] =&GT;排列
                            (
                            )                        [追加:保护] =&GT;排列
                            (
                            )                        [守卫:保护] =&GT;排列
                            (
                                [0] =&GT; *
                            )                        [日期:保护] =&GT;排列
                            (
                            )                        [日期格式:保护] =&GT;
                        [蒙上:保护] =&GT;排列
                            (
                            )                        [触摸:保护] =&GT;排列
                            (
                            )                        [观测:保护] =&GT;排列
                            (
                            )                        [有:保护] =&GT;排列
                            (
                            )                        [morphClass:保护] =&GT;
                        [存在] =&GT; 1
                        [wasRecentlyCreated] =&GT;
                    )            )    )[2] =&GT;照亮\\数据库\\锋\\集合对象
    (
        [项目:保护] =&GT;排列
            (
            )    ))

编辑2

我的print_r文章从$ =篇文章::所有();它使用相同的回报视图(pages.articles',紧凑的('文章'));而在同一网站上显示:

 照亮\\数据库\\锋\\集合对象

[项目:保护] =&GT;排列
    (
        [0] =&GT;应用程序\\对象条
            (
                [可填写:保护] =&GT;排列
                    (
                        ...
                    )                [连接:保护] =&GT;
                [表:保护] =&GT;
                [PrimaryKey的:保护] =&GT; ID
                [perPage:保护] =&GT; 15
                [递增] =&GT; 1
                [时间戳] =&GT; 1
                [属性:保护] =&GT;排列
                    (
                        ...
                    )                [原创:保护] =&GT;排列
                    (
                        ...
                    )                [关系:保护] =&GT;排列
                    (
                    )                [隐藏:保护] =&GT;排列
                    (
                    )                [可见:保护] =&GT;排列
                    (
                    )                [追加:保护] =&GT;排列
                    (
                    )                [守卫:保护] =&GT;排列
                    (
                        [0] =&GT; *
                    )                [日期:保护] =&GT;排列
                    (
                    )                [日期格式:保护] =&GT;
                [蒙上:保护] =&GT;排列
                    (
                    )                [触摸:保护] =&GT;排列
                    (
                    )                [观测:保护] =&GT;排列
                    (
                    )                [有:保护] =&GT;排列
                    (
                    )                [morphClass:保护] =&GT;
                [存在] =&GT; 1
                [wasRecentlyCreated] =&GT;
            )        [1] =&GT;应用程序\\对象条
            (
                [可填写:保护] =&GT;排列
                    (
                        ..
                    )                [连接:保护] =&GT;
                [表:保护] =&GT;
                [PrimaryKey的:保护] =&GT; ID
                [perPage:保护] =&GT; 15
                [递增] =&GT; 1
                [时间戳] =&GT; 1
                [属性:保护] =&GT;排列
                    (
                        ...
                    )                [原创:保护] =&GT;排列
                    (
                        ..
                    )                [关系:保护] =&GT;排列
                    (
                    )                [隐藏:保护] =&GT;排列
                    (
                    )                [可见:保护] =&GT;排列
                    (
                    )                [追加:保护] =&GT;排列
                    (
                    )                [守卫:保护] =&GT;排列
                    (
                        [0] =&GT; *
                    )                [日期:保护] =&GT;排列
                    (
                    )                [日期格式:保护] =&GT;
                [蒙上:保护] =&GT;排列
                    (
                    )                [触摸:保护] =&GT;排列
                    (
                    )                [观测:保护] =&GT;排列
                    (
                    )                [有:保护] =&GT;排列
                    (
                    )                [morphClass:保护] =&GT;
                [存在] =&GT; 1
                [wasRecentlyCreated] =&GT;
            )        [2] =&GT;应用程序\\对象条
            (
                [可填写:保护] =&GT;排列
                    (
                        ..
                    )                [连接:保护] =&GT;
                [表:保护] =&GT;
                [PrimaryKey的:保护] =&GT; ID
                [perPage:保护] =&GT; 15
                [递增] =&GT; 1
                [时间戳] =&GT; 1
                [属性:保护] =&GT;排列
                    (..
                    )                [原创:保护] =&GT;排列
                    (
                        ..
                    )                [关系:保护] =&GT;排列
                    (
                    )                [隐藏:保护] =&GT;排列
                    (
                    )                [可见:保护] =&GT;排列
                    (
                    )                [追加:保护] =&GT;排列
                    (
                    )                [守卫:保护] =&GT;排列
                    (
                        [0] =&GT; *
                    )                [日期:保护] =&GT;排列
                    (
                    )                [日期格式:保护] =&GT;
                [蒙上:保护] =&GT;排列
                    (
                    )                [触摸:保护] =&GT;排列
                    (
                    )                [观测:保护] =&GT;排列
                    (
                    )                [有:保护] =&GT;排列
                    (
                    )                [morphClass:保护] =&GT;
                [存在] =&GT; 1
                [wasRecentlyCreated] =&GT;
            )    ))

所以从我的print_r输出($文章)应该是这样的。

编辑3

我目前的foreach是:

 的foreach($拉链codeS为$关键=&GT; $值)
        {
            // $code = $拉链codeS [0] - &GT; zc_zip; //工作,打印出一文
            // $文章=文章::在哪里('拉链code',$code) - &GT;获得();
            $文章= ::条的地方('拉链code',$值 - &GT; zc_zip) - GT;获得();
            //的print_r($条);
        }
        的print_r($条);


解决方案

您可以试试这个:

 的foreach($拉链codeS为$关键=&GT; $值)
{
    $文章[] =文章::在哪里('拉链code',$值 - &GT; zc_zip) - GT;获得();
}
返回视图(pages.articles',紧凑的('文章'));

查看:

  @foreach($文章为$文)
&lt;物品&GT;
    &LT; H2&GT;
        &LT; A HREF ={!! URL('/篇',$物品─&GT; ID)!}&GT; {! $物品─&GT;的名字!}&LT; / A&GT;
    &LT; / H&GT;    &LT; D​​IV&GT;
        &LT;标签&gt;简介:LT; /标签&gt;
        &LT; P&GT; {! $物品─&GT;说明!}&LT; / P&GT;
    &LT; / DIV&GT;
    &LT; D​​IV&GT;
        &LT;标签&gt; ZIP code:&LT; /标签&gt;
        {! $物品─&GT;拉链code!}
    &LT; / DIV&GT;
    &LT; D​​IV&GT;
        &LT;标签&gt;发布时间:LT; /标签&gt;
        {! $物品─&GT;公布!}
    &LT; / DIV&GT;
    &LT; D​​IV&GT;
        &LT;标签&gt;等级:LT; /标签&gt;
        {! $物品─&GT;排名!}
    &LT; / DIV&GT;
&LT; /条&GT;
&LT;小时/&GT;
@endforeach

I have a problem in my Laravel 5 application. My controller-method contains a foreach, which should iterate over a previous Eloquent-result-array (it is named $zipcodes). So each zipcode is used to fill a query, which result returns an Article-object (named $article). Every end of iteration it should add the Article-result to an array ($articles), which is used at the end of my method to display the Articles on my page. Maybe there is a better option for the performing of the Article-query, not every iteration, but I don't know how.

UPDATE

This is working for my problem, without perform the query in a foreach: Laravel multiple where clauses in query from given array

My code now is

// grabs all zipcodes matching the distance
$zipcodes = $this->getZipcodes($zipCoordinateId, $distance);

foreach ($zipcodes AS $key=>$val)
{
    $zipcodes[$key] = (array) $val;
}

$codes = array_column($zipcodes, 'zc_zip');

$articles = Article::whereIn('zipcode', $codes)->get();

return view('pages.intern.articles.index', compact('articles'));

UPDATE END

My SearchController:

<?php

namespace App\Http\Controllers;

use App\Article;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class SearchController extends Controller
{
public function getSearch(Request $request)
{
    if(($request->has('zipcode'))) {
        $zipcode = $request->input('zipcode');
        $distance = $request->input('distance');

        $zipCoordinateId = $this->getZipCoordindateId($zipcode);
        $zipcodes = $this->getZipcodes($zipCoordinateId, $distance);

        $articles = array();

        foreach($zipcodes as $value) {
            //$zipcode = $zipcodes[0]->zc_zip; // this is working
            $zipcode = $value->zc_zip;

            $article = Article::where('zipcode', $zipcode)->get();


           // add the $article each iteration to $articles
        }

        return view('pages.articles', compact('articles'));
    } else {
        return redirect('/articles');
    }
}

public function getZipCoordindateId($value) {
    $result = DB::table('zip_coordinates')
            ->where('zc_zip', '=' , $value)
            ->orWhere('zc_location_name', 'LIKE', $value)
            ->pluck('zc_id');
    return $result;
}

public function getZipcodes($id, $distance) {

    $result = DB::select(
        DB::raw("
            SELECT dest.zc_zip,
                ACOS(
                    SIN(RADIANS(src.zc_lat)) * SIN(RADIANS(dest.zc_lat))
                    + COS(RADIANS(src.zc_lat)) * COS(RADIANS(dest.zc_lat))
                    * COS(RADIANS(src.zc_lon) - RADIANS(dest.zc_lon))
                ) * 6380 AS distance
            FROM zip_coordinates AS dest
            CROSS JOIN zip_coordinates AS src
            WHERE src.zc_id = :id
            HAVING distance < :distance
            ORDER BY distance"), array('id' => $id, 'distance' => $distance));

    return $result;
}
}

my view:

@extends('layouts.default')

@section('content')
    <h1>All Articles</h1>
<hr/>

<search>
    {!! Form::open(['url' => 'intern/search']) !!}
        {!! Form::text('zipcode', null, ['class' => 'form-control']) !!}
        {!! Form::select('distance', [
          '5' => '5 km',
          '10' => '10 km',
          '25' => '25 km',
          '50' => '50 km',
          ], null, ['class' => 'form-control']) !!}
        {!! Form::submit('Suchen', ['class' => 'btn btn-success']) !!}
    {!! Form::close() !!}
</search>


@foreach($articles as $article)
    <article>
        <h2>
            <a href="{{ url('/articles', $article->id) }}">{{ $article->name }}</a>
        </h2>

        <div>
            <label>Description:</label>
            <p>{{ $article->description }}</p>
        </div>
        <div>
            <label>ZIPCODE:</label>
            {{ $article->zipcode }}
        </div>
        <div>
            <label>Published:</label>
            {{ $article->published }}
        </div>
        <div>
            <label>Rank:</label>
            {{ $article->rank }}
        </div>
    </article>
    <hr/>
@endforeach
@stop

Hope you can help me, thank you in advance. quantatheist

EDIT

When I use

foreach($zipcodes as $key => $value)
        {
            $articles[] = Article::where('zipcode', $value->zc_zip)->get();
        }
        print_r($articles);

With another zipcode in the search and updated zipcodes of my articles, it prints (at the moment, there are three articles in my database, the both first have zipcodes, which are in the range of my new zipcode-search):

Array
(
[0] => Illuminate\Database\Eloquent\Collection Object
    (
        [items:protected] => Array
            (
                [0] => App\Article Object
                    (
                        [fillable:protected] => Array
                            (
                                [0] => name
                                [1] => description
                                [2] => profile
                                [3] => driverlicense
                                [4] => zipcode
                                [5] => published
                                [6] => rank
                                [7] => contact
                                [8] => note
                                [9] => pictures
                                [10] => publisher
                            )

                        [connection:protected] => 
                        [table:protected] => 
                        [primaryKey:protected] => id
                        [perPage:protected] => 15
                        [incrementing] => 1
                        [timestamps] => 1
                        [attributes:protected] => Array
                            (
                                [id] => 5
                                [name] => Test1
                                [description] => Lorem ipsum dolor sit amet, consetetur 
                                [profile] => Lorem ipsum dolor sit amet, consetetur
                                [driverlicense] => yes
                                [published] => no
                                [rank] => 10
                                [contact] => 1
                                [pictures] => 
                                [zipcode] => 89429
                                [note] => 
                                [publisher] => 1
                                [created_at] => 2015-11-04 01:45:18
                                [updated_at] => 2015-11-04 10:07:24
                            )

                        [original:protected] => Array
                            (
                                [id] => 5
                                [name] => Test1
                                [description] => Lorem ipsum dolor sit amet, consetetur
                                [profile] => Lorem ipsum dolor sit amet, consetetur
                                [driverlicense] => yes
                                [published] => no
                                [rank] => 10
                                [contact] => 1
                                [pictures] => 
                                [zipcode] => 89429
                                [note] => 
                                [publisher] => 1
                                [created_at] => 2015-11-04 01:45:18
                                [updated_at] => 2015-11-04 10:07:24
                            )

                        [relations:protected] => Array
                            (
                            )

                        [hidden:protected] => Array
                            (
                            )

                        [visible:protected] => Array
                            (
                            )

                        [appends:protected] => Array
                            (
                            )

                        [guarded:protected] => Array
                            (
                                [0] => *
                            )

                        [dates:protected] => Array
                            (
                            )

                        [dateFormat:protected] => 
                        [casts:protected] => Array
                            (
                            )

                        [touches:protected] => Array
                            (
                            )

                        [observables:protected] => Array
                            (
                            )

                        [with:protected] => Array
                            (
                            )

                        [morphClass:protected] => 
                        [exists] => 1
                        [wasRecentlyCreated] => 
                    )

            )

    )

[1] => Illuminate\Database\Eloquent\Collection Object
    (
        [items:protected] => Array
            (
                [0] => App\Article Object
                    (
                        [fillable:protected] => Array
                            (
                                [0] => name
                                [1] => description
                                [2] => profile
                                [3] => driverlicense
                                [4] => zipcode
                                [5] => published
                                [6] => rank
                                [7] => contact
                                [8] => note
                                [9] => pictures
                                [10] => publisher
                            )

                        [connection:protected] => 
                        [table:protected] => 
                        [primaryKey:protected] => id
                        [perPage:protected] => 15
                        [incrementing] => 1
                        [timestamps] => 1
                        [attributes:protected] => Array
                            (
                                [id] => 6
                                [name] => Test2
                                [description] => Lorem ipsum dolor sit amet, consetetur 
                                [profile] => Lorem ipsum dolor sit amet
                                [driverlicense] => yes
                                [published] => no
                                [rank] => 3
                                [contact] => 1
                                [pictures] => 
                                [zipcode] => 89428
                                [note] => 
                                [publisher] => 1
                                [created_at] => 2015-11-04 01:45:38
                                [updated_at] => 2015-11-04 09:58:01
                            )

                        [original:protected] => Array
                            (
                                [id] => 6
                                [name] => Test2
                                [description] => Lorem ipsum dolor sit amet, consetetur 
                                [profile] => Lorem ipsum dolor sit amet
                                [driverlicense] => yes
                                [published] => no
                                [rank] => 3
                                [contact] => 1
                                [pictures] => 
                                [zipcode] => 89428
                                [note] => 
                                [publisher] => 1
                                [created_at] => 2015-11-04 01:45:38
                                [updated_at] => 2015-11-04 09:58:01
                            )

                        [relations:protected] => Array
                            (
                            )

                        [hidden:protected] => Array
                            (
                            )

                        [visible:protected] => Array
                            (
                            )

                        [appends:protected] => Array
                            (
                            )

                        [guarded:protected] => Array
                            (
                                [0] => *
                            )

                        [dates:protected] => Array
                            (
                            )

                        [dateFormat:protected] => 
                        [casts:protected] => Array
                            (
                            )

                        [touches:protected] => Array
                            (
                            )

                        [observables:protected] => Array
                            (
                            )

                        [with:protected] => Array
                            (
                            )

                        [morphClass:protected] => 
                        [exists] => 1
                        [wasRecentlyCreated] => 
                    )

            )

    )

[2] => Illuminate\Database\Eloquent\Collection Object
    (
        [items:protected] => Array
            (
            )

    )

)

EDIT 2

I print_r the articles from $articles = Article::all();, which uses the same return view('pages.articles', compact('articles')); and displays on the same site:

Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
    (
        [0] => App\Article Object
            (
                [fillable:protected] => Array
                    (
                        ...
                    )

                [connection:protected] => 
                [table:protected] => 
                [primaryKey:protected] => id
                [perPage:protected] => 15
                [incrementing] => 1
                [timestamps] => 1
                [attributes:protected] => Array
                    (
                        ...
                    )

                [original:protected] => Array
                    (
                        ...
                    )

                [relations:protected] => Array
                    (
                    )

                [hidden:protected] => Array
                    (
                    )

                [visible:protected] => Array
                    (
                    )

                [appends:protected] => Array
                    (
                    )

                [guarded:protected] => Array
                    (
                        [0] => *
                    )

                [dates:protected] => Array
                    (
                    )

                [dateFormat:protected] => 
                [casts:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [with:protected] => Array
                    (
                    )

                [morphClass:protected] => 
                [exists] => 1
                [wasRecentlyCreated] => 
            )

        [1] => App\Article Object
            (
                [fillable:protected] => Array
                    (
                        ..
                    )

                [connection:protected] => 
                [table:protected] => 
                [primaryKey:protected] => id
                [perPage:protected] => 15
                [incrementing] => 1
                [timestamps] => 1
                [attributes:protected] => Array
                    (
                        ...
                    )

                [original:protected] => Array
                    (
                        ..
                    )

                [relations:protected] => Array
                    (
                    )

                [hidden:protected] => Array
                    (
                    )

                [visible:protected] => Array
                    (
                    )

                [appends:protected] => Array
                    (
                    )

                [guarded:protected] => Array
                    (
                        [0] => *
                    )

                [dates:protected] => Array
                    (
                    )

                [dateFormat:protected] => 
                [casts:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [with:protected] => Array
                    (
                    )

                [morphClass:protected] => 
                [exists] => 1
                [wasRecentlyCreated] => 
            )

        [2] => App\Article Object
            (
                [fillable:protected] => Array
                    (
                        ..
                    )

                [connection:protected] => 
                [table:protected] => 
                [primaryKey:protected] => id
                [perPage:protected] => 15
                [incrementing] => 1
                [timestamps] => 1
                [attributes:protected] => Array
                    (..
                    )

                [original:protected] => Array
                    (
                        ..
                    )

                [relations:protected] => Array
                    (
                    )

                [hidden:protected] => Array
                    (
                    )

                [visible:protected] => Array
                    (
                    )

                [appends:protected] => Array
                    (
                    )

                [guarded:protected] => Array
                    (
                        [0] => *
                    )

                [dates:protected] => Array
                    (
                    )

                [dateFormat:protected] => 
                [casts:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [with:protected] => Array
                    (
                    )

                [morphClass:protected] => 
                [exists] => 1
                [wasRecentlyCreated] => 
            )

    )

)

So the output from my print_r($articles) should be something like this.

EDIT 3

My current foreach is:

foreach($zipcodes as $key => $value)
        {
            //$code = $zipcodes[0]->zc_zip; // working, prints out one article
            //$article = Article::where('zipcode', $code)->get();
            $article = Article::where('zipcode', $value->zc_zip)->get();
            //print_r($article);
        }
        print_r($article);

解决方案

You could try this:

foreach($zipcodes as $key => $value)
{
    $articles[] = Article::where('zipcode', $value->zc_zip)->get();
}
return view('pages.articles', compact('articles'));

View:

@foreach($articles as $article)
<article>
    <h2>
        <a href="{!! url('/articles', $article->id) !!}">{!! $article->name !!}</a>
    </h2>

    <div>
        <label>Description:</label>
        <p>{!! $article->description !!}</p>
    </div>
    <div>
        <label>ZIPCODE:</label>
        {!! $article->zipcode !!}
    </div>
    <div>
        <label>Published:</label>
        {!! $article->published !!}
    </div>
    <div>
        <label>Rank:</label>
        {!! $article->rank !!}
    </div>
</article>
<hr/>
@endforeach

这篇关于Laravel 5加从查询结果在foreach数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆