Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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 有说服力的质询在拉雷维尔太慢了_Php_Laravel_Eloquent_Laravel 5.3 - Fatal编程技术网

Php 有说服力的质询在拉雷维尔太慢了

Php 有说服力的质询在拉雷维尔太慢了,php,laravel,eloquent,laravel-5.3,Php,Laravel,Eloquent,Laravel 5.3,我在Laravel 5.3.19中做一个应用程序,问题是,我正在用Elount做一个查询,但它太慢了,我读了一些论坛,似乎我做错了什么,我试着在控制台中用命令php artisan tinker来做这个查询,查询工作非常好,问题是,当我从浏览器登录到我在routes/web.php中创建的URL时,页面只是无限期加载,有时会给我一个超时错误。有人知道可能是什么问题吗 这是我的档案: routes/web.php Http/controllers/AreaProductController

我在Laravel 5.3.19中做一个应用程序,问题是,我正在用Elount做一个查询,但它太慢了,我读了一些论坛,似乎我做错了什么,我试着在控制台中用命令
php artisan tinker
来做这个查询,查询工作非常好,问题是,当我从浏览器登录到我在
routes/web.php
中创建的URL时,页面只是无限期加载,有时会给我一个超时错误。有人知道可能是什么问题吗

这是我的档案:

  • routes/web.php

  • Http/controllers/AreaProductController.php(控制器)
公共功能订单ProductsListByArea($area){
$array_products=[];
$products=AreaProduct::where('area_id',$area)->get();
foreach($产品作为$数据){
$product=[
'id'=>$data->id,
“价格”=>$data->price,
'product_id'=>$data->product->id,
“产品名称”=>$data->product->name,
“产品类别”=>$data->product->category->name,
“产品参考”=>$data->product->reference,
“产品代码”=>$data->product->code,
'area\u id'=>$data->area\u id
];
阵列推送($array\u products,$product);
}
}    
  • Http/AreaProduct.php(模型)
注意:这个查询应该会返回大约700个结果,我注意到这个问题是在处理关系时产生的,比如
'product\u name'=>$data->product->name
。我做错什么了吗


谢谢您的帮助。

既然您使用的是雄辩的人际关系,您就不能这样做:

public function orderProductsListByArea($area){
    // Load the Area products with the 'product' relationship (eager loading)
    $products = AreaProduct::where('area_id', $area)
        ->with('product')
        ->get();

    // Return the list of products
    return $products;
}

在看不到其余数据/db的情况下,我很确定它会返回您想要的数据。

既然您使用的是雄辩的关系,您就不能这样做:

public function orderProductsListByArea($area){
    // Load the Area products with the 'product' relationship (eager loading)
    $products = AreaProduct::where('area_id', $area)
        ->with('product')
        ->get();

    // Return the list of products
    return $products;
}

在没有看到其余数据/db的情况下,我很确定这应该会返回您想要的数据。

foreach($products as$data){
,您的代码将看到一些地方..例如超时..因为您无情地使用了延迟加载。您应该更改
$products=AreaProduct::where('area_id',$area)->get()
$products=AreaProduct::where('area_id',$area)->with('product')->get();
以急切地加载关系。非常感谢您,Bagus!
foreach($products as$data){
,您的代码将看到一些地方..例如超时..因为您无情地使用惰性加载。您应该更改
$products=AreaProduct::where('area_id',$area)->get();
to
$products=AreaProduct::where('area_id',$area)->with('product')->get()
渴望加载关系。非常感谢,Bagus!非常感谢!在查询中甚至不需要2秒钟,再次,非常感谢,这正是我需要的谢谢!在查询中甚至不需要2秒钟,再次,非常感谢,这正是我需要的
public function orderProductsListByArea($area){
    // Load the Area products with the 'product' relationship (eager loading)
    $products = AreaProduct::where('area_id', $area)
        ->with('product')
        ->get();

    // Return the list of products
    return $products;
}