laravel 5.4 嵌套 foreach 循环中的重复查询

laravel 5.4 duplicates of queries within a nested foreach loop

在我的 Laravel 5.4 项目中,我试图从模型 Article 和 Status 之间的关系中获取一些数据。关系类型是 OneToMany,其中文章有一个状态,状态有很多文章。

为了获取想要的数据,我在方法 whereHas() 的帮助下遍历了模型文章项目的集合,这些项目与 model/relationship 状态一起被 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>

回显 $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` = ?)

第三次迭代:

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 迭代并在前一个迭代的末尾附加一些查询。如果您能给我一些建议,我将不胜感激。

谢谢

您必须从 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>