Php Laravel 5将foreach中的查询结果添加到数组中
我的Laravel 5应用程序有问题。 我的控制器方法包含一个foreach,它应该迭代前面的雄辩的结果数组(名为$zipcodes)。因此,每个zipcode都用于填充一个查询,查询结果返回一个Article对象(名为$Article)。 每次迭代结束时,它都应该将文章结果添加到一个数组($articles),该数组在我的方法结束时用于在我的页面上显示文章。 也许有更好的方法来执行文章查询,而不是每次迭代,但我不知道如何执行 更新 这解决了我的问题,无需在foreach中执行查询: 我现在的代码是Php Laravel 5将foreach中的查询结果添加到数组中,php,arrays,laravel,laravel-5,add,Php,Arrays,Laravel,Laravel 5,Add,我的Laravel 5应用程序有问题。 我的控制器方法包含一个foreach,它应该迭代前面的雄辩的结果数组(名为$zipcodes)。因此,每个zipcode都用于填充一个查询,查询结果返回一个Article对象(名为$Article)。 每次迭代结束时,它都应该将文章结果添加到一个数组($articles),该数组在我的方法结束时用于在我的页面上显示文章。 也许有更好的方法来执行文章查询,而不是每次迭代,但我不知道如何执行 更新 这解决了我的问题,无需在foreach中执行查询: 我现在的代
// 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'));
更新结束
我的搜索控制器:
<?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;
}
}
使用搜索中的另一个zipcode和我文章的更新zipcode,它会打印(目前,我的数据库中有三篇文章,第一篇和第二篇都有zipcode,它们在我的新zipcode搜索范围内):
编辑2
我打印$articles=Article::all()中的文章;,使用相同的返回视图('pages.articles',compact('articles');并在同一站点上显示:
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] =>
)
)
)
所以我的print_r($articles)的输出应该是这样的
编辑3
我目前的工作是:
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);
你可以试试这个:
foreach($zipcodes as $key => $value)
{
$articles[] = Article::where('zipcode', $value->zc_zip)->get();
}
return view('pages.articles', compact('articles'));
视图:
@foreach($articles作为$article)
说明:
{!!$article->description!!}
ZIPCODE:
{!!$article->zipcode!!}
出版:
{!!$article->published!!}
排名:
{!!$article->rank!!}
@endforeach
您可以尝试以下方法:
foreach($zipcodes as $key => $value)
{
$articles[] = Article::where('zipcode', $value->zc_zip)->get();
}
return view('pages.articles', compact('articles'));
视图:
@foreach($articles作为$article)
说明:
{!!$article->description!!}
ZIPCODE:
{!!$article->zipcode!!}
出版:
{!!$article->published!!}
排名:
{!!$article->rank!!}
@endforeach
您已声明$articles=array();然后您将$article(不带s)传递给compact函数,这就是为什么您在视图中得到一个未定义的变量,而且当您在foreach中循环时,您必须这样做:
@foreach($articles as $article)
....
@endforeach
您已声明$articles=array();然后您将$article(不带s)传递给compact函数,这就是为什么您在视图中得到一个未定义的变量,而且当您在foreach中循环时,您必须这样做:
@foreach($articles as $article)
....
@endforeach
你能打印($zipcodes)吗?是的,它显示:数组([0]=>stdClass对象([zc_-zip]=>97204[distance]=>3.9266259510316)[1]=>stdClass对象([zc_-zip]=>97218[distance]=>4.362292988114)[2]=>stdClass对象([zc_-zip]=>97236[距离]=>4.5967044919437)编辑:三项是正确的,它们在5的距离内。如果可以的话,请尝试我的答案。你能打印($zipcodes)吗?是的,它显示:数组([0]=>stdClass对象([zc\u zip]=>97204[distance]=>3.9266259510316)[1]=>stdClass对象([zc\u zip]=>97218[distance]=>4.3622988114)[2]=>stdClass对象([zc_zip]=>97236[distance]=>4.5967044919437))编辑:三个项目是正确的,它们的距离为5。如果我的答案行得通的话,请回答我的问题。我说得对吗,像$articles这样的东西(我的契约)不见了?不,不工作。你能再打印一次吗?stdClass对象([zc_-zip]=>97204[distance]=>3.9266259510316)stdClass对象([zc_-zip]=>97218[distance]=>4.362292988114)stdClass对象([zc_-zip]=>97236[distance]=>4.5967044919437)试试我的答案。我认为你的合同错了。所以我修改了它。对不起,忘了谈论我的观点。它应该显示数据库中的所有文章。所以它有一个@foreach($articles as$article)@EndForeachy我说得对吗,比如$articles(我的紧凑型)丢失了?不,不工作。你能再打印一次吗?stdClass对象([zc_-zip]=>97204[distance]=>3.9266259510316)stdClass对象([zc_-zip]=>97218[distance]=>4.362292988114)stdClass对象([zc_-zip]=>97236[distance]=>4.5967044919437)试试我的答案。我认为你的合同错了。所以我修改了它。对不起,忘了谈论我的观点。它应该显示数据库中的所有文章。所以它有一个@foreach($articles as$article)@抱歉,这是从编辑我的代码到aldrin27的解决方案。正如你所写,它就在那里@你现在犯了什么错误?我仍然看到getSearch方法中有$article,它必须是
$articles[]=article::where('zipcode',$zipcode)->get()代码>否则,您将返回一个空数组。使用您的解决方案,我将得到一个“Undefined property:Illumb\Database\Eloquent\Collection::$id”-ErrorException。抱歉,这是从编辑我的代码到aldrin27的解决方案。正如你所写,它就在那里@你现在犯了什么错误?我仍然看到getSearch方法中有$article,它必须是$articles[]=article::where('zipcode',$zipcode)->get()代码>否则,您将返回一个空数组。使用您的解决方案,我将得到一个“Undefined property:Illumb\Database\Eloquent\Collection::$id”-ErrorException。
@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
@foreach($articles as $article)
....
@endforeach