Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php laravel 5.4嵌套foreach循环中的重复查询_Php_Mysql_Laravel_Foreach_Laravel 5.4 - Fatal编程技术网

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

Php laravel 5.4嵌套foreach循环中的重复查询,php,mysql,laravel,foreach,laravel-5.4,Php,Mysql,Laravel,Foreach,Laravel 5.4,在我的Laravel5.4项目中,我试图从模型文章和状态之间的关系中获取一些数据。关系类型为OneToMany,其中Article具有一个状态,Status具有多个项目 为了获取想要的数据,我通过一个方法whereHas()迭代了一组模型文章项,这些项目与模型/关系状态一起加载。第一次迭代构建一个正确的查询,但每次迭代都会附加由方法whereHas()[我猜是这样]生成的查询 如果给定-> 文章范本: class Article extends Model { public functio

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

为了获取想要的数据,我通过一个方法
whereHas()
迭代了一组模型文章项,这些项目与模型/关系状态一起加载。第一次迭代构建一个正确的查询,但每次迭代都会附加由方法
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);
 }
}
将变量通过控制器传递到视图:

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>
第二次迭代:

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` = ?)
第三次迭代:

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;
}
这样,您就有了一个属性status为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>

@foreach($status=>$articles作为最终结果)
#
提图洛克
Vytvorené
公共图书馆
Upravené
行动

{{$status}

@foreach($articles作为$article) //下面是显示文章属性的代码 @endforeach @endforeach
您好,非常感谢您的解决方案,它工作正常。就为了记录,$count变量必须被删除,因为它没有被声明为amd替换,例如$loop->index(laravel的变量)。。