在我的Laravel 5.4项目中,我试图从模型文章和状态之间的关系中获取一些数据。关系类型是OneToMany,其中Article有一个Status,Status有很多文章。
要获取想要的数据,我会在方法whereHas()
的帮助下遍历模型文章项的集合,这些项目项目与模型/关系状态一起被eagerLoaded。第一次迭代构建一个正确的查询,但是在彼此迭代的情况下,它会附加方法whereHas()
生成的查询[我的猜测]。
如果给出 - >
,我该如何解决这个问题(?)文章模型:
class Article extends Model
{
public function status()
{
return $this->belongsTo(ArticleStatus::class,'articleStatus_id');
}
}
状态模型:
class ArticleStatus extends Model
{
public function article()
{
return $this->hasMany(Article::class);
}
}
通过Controller将变量传递给视图:
class RedactionController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$articles = Auth::user()->articles();
$statuses = array_values(ArticleStatus::all()->pluck('status')->toArray());
return view('redaction',[
'articles' => $articles,
'statuses' => $statuses,
]);
}
我希望迭代数据的部分视图,并根据文章的状态显示它们:
<div class="tab-content">
@foreach($statuses as $count => $status)
<div class="card-block tab-pane @if($count==0) active @endif" id="{{$status}}">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Titulok</th>
<th>Vytvorené</th>
<th>Publikované</th>
<th>Upravené</th>
<th class="text-center">Action</th>
</tr>
</thead>
<tbody>
<p class="mt-5"><b>{{$status}}</b></p>
@php
$result = $articles->with('status')->whereHas('status', function ($query) use ($status)
{
$query->where('status','=', $status);
})->toSql();
echo $result;
@endphp
</tbody>
</table>
</div>
@endforeach
</div>
echo $ result变量形式的结果:
第一次迭代:
select * from `articles` where `articles`.`user_id` = ? and `articles`.`user_id` is not null and exists (select * from `article_statuses` where `articles`.`articleStatus_id` = `article_statuses`.`id` and `status` = ?)
第二次迭代:
select * from `articles` where `articles`.`user_id` = ? and `articles`.`user_id` is not null and exists (select * from `article_statuses` where `articles`.`articleStatus_id` = `article_statuses`.`id` and `status` = ?) and exists (select * from `article_statuses` where `articles`.`articleStatus_id` = `article_statuses`.`id` and `status` = ?)
第3次迭代:
select * from `articles` where `articles`.`user_id` = ? and `articles`.`user_id` is not null and exists (select * from `article_statuses` where `articles`.`articleStatus_id` = `article_statuses`.`id` and `status` = ?) and exists (select * from `article_statuses` where `articles`.`articleStatus_id` = `article_statuses`.`id` and `status` = ?) and exists (select * from `article_statuses` where `articles`.`articleStatus_id` = `article_statuses`.`id` and `status` = ?)
因此,查询会构建每个foreach迭代,并在前一个查询的末尾附加一些查询。如果你能给我一些建议,我会感恩。
感谢
答案 0 :(得分:0)
您必须从foreach中取出查询并将其放入索引函数中,例如
$results = $articles->with('status')->whereHas('status', function ($query) use ($statuses)
{
$query->whereIn('status', $statuses);
})->get();
这将获得所有状态均为$statuses
的文章,然后您可以为您的视图制作一个结构,如
$final_results = [];
foreach($results as $article)
{
if( ! isset($final_results[$article->status->status]))
$final_results[$article->status->status] = [];
$final_results[$article->status->status][] = $article;
}
有了这个,你有一个属性状态为ArticleStatus的数组作为其文章的键,将$final_results
变量传递给你的视图。
然后在你看来
<div class="tab-content">
@foreach($final_results as $status => $articles)
<div class="card-block tab-pane @if($count==0) active @endif" id="{{$status}}">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Titulok</th>
<th>Vytvorené</th>
<th>Publikované</th>
<th>Upravené</th>
<th class="text-center">Action</th>
</tr>
</thead>
<tbody>
<p class="mt-5"><b>{{$status}}</b></p>
@foreach($articles as $article)
// Here your code to show Article properties
@endforeach
</tbody>
</table>
</div>
@endforeach
</div>