2017-11-01 04:07
浏览 360


I'm doing an application in Laravel 5.3.19, the thing is, I'm doing a query with Eloquent but it is SO slow, I read some forums and seems like I'm doing something wrong, I tried doing this query in the console with the command php artisan tinker and the query work perfectly, the problem is when I log into the URL that I created in the routes/web.php from my browser, the page just load indefinitely and sometimes It gives to me a timeout error. Someone knows what could be the problem?

here are my files:

  • routes/web.php

  • Http/controllers/AreaProductController.php (Controller)
public function orderProductsListByArea($area){
    $array_products = [];
    $products = AreaProduct::where('area_id', $area)->get();
    foreach ($products as $data) {
        $product = [
            'id' => $data->id,
            'price' => $data->price,
            'product_id' => $data->product->id,
            'product_name' => $data->product->name,
            'product_category' => $data->product->category->name,
            'product_reference' => $data->product->reference,
            'product_code' => $data->product->code,
            'area_id' => $data->area_id
        array_push($array_products, $product);
  • Http/AreaProduct.php (Model)
namespace App;
use Illuminate\Database\Eloquent\Model;
class AreaProduct extends Model{
    public $table = "products_area";
    public function product(){
        return $this->belongsTo('App\Product');
    public function area(){
        return $this->belongsTo('App\Area');

Note: This query should return near to 700 results, I noticed the problem is produced when I'm doing the relations stuff like 'product_name' => $data->product->name. Am I doing something wrong?

Thanks for your help.

图片转代码服务由CSDN问答提供 功能建议

我正在Laravel 5.3.19中做一个应用程序,问题是,我正在用Eloquent进行查询 但它很慢,我读了一些论坛,似乎我做错了什么,我尝试在控制台中使用命令 php artisan tinker 进行此查询,查询工作完美,问题是 当我从浏览器登录我在 routes / web.php 中创建的URL时,页面只是无限加载,有时它会给我一个超时错误。 有人知道可能是什么问题?


  • routes / web.php \ ñ

     <代码>路线::得到( '/ area_products / listProductsByArea / {面积}', 'AreaProductController @ orderProductsListByArea');  
    • Http / controllers / AreaProductController.php(Controller)
        public function orderProductsListByArea($ area){
       $ array_products = []; 
       $ products = AreaProduct :: where('area_id',$ area) -  &gt; get(); 
       foreach($ products as $ data){
       $ product = [
      'id'=&gt;  $ data-&gt; id,
      'price'=&gt;  $ data-&gt; price,
      'product_id'=&gt;  $ data-&gt; product-&gt; id,
      'product_name'=&gt;  $ data-&gt; product-&gt; name,
      'product_category'=&gt;  $ data-&gt; product-&gt; category-&gt; name,
      'product_reference'=&gt;  $ data-&gt; product-&gt; reference,
      'product_code'=&gt;  $ data-&gt; product-&gt; code,
      'area_id'=&gt;  $ data-&gt; area_id 
       array_push($ array_products,$ product); 
      • Http /AreaProduct.php(Model)
          namespace App; 
        use Illuminate \ Database \ Eloquent \ Model; 
        class AreaProduct extends Model {  
         public $ table =“products_area”; 
        返回$ this-&gt; belongsTo('App \ Product'); 
        返回$  this-&gt; belongsTo('App \ Area'); 

        注意:此查询应返回接近700个结果 ,我注意到当我正在进行像'product_name'=&gt;这样的关系时会产生问题。 $ DATA-&GT;产品 - &GT;名称。 难道我做错了什么?


  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doubei2231 2017-11-01 04:45

    Since you're using Eloquent relationships can't you just do this:

    public function orderProductsListByArea($area){
        // Load the Area products with the 'product' relationship (eager loading)
        $products = AreaProduct::where('area_id', $area)
        // Return the list of products
        return $products;

    Without seeing the rest of your data/db, I'm pretty sure that should return what you want.

    解决 无用
    打赏 举报

相关推荐 更多相似问题