【问题标题】:Count function return 0 in laravel计数函数在laravel中返回0
【发布时间】:2019-12-26 06:25:18
【问题描述】:

我想显示使用 count 函数的公司的产品数量,但它返回 0 为什么? 关系的真正语法是什么? 以及如何从模型的函数中获取 sql 查询?

Agent_Company.php

class Agent_Company extends Model
{
    //
    protected $table='agent_companies';
    protected $primaryKey='id';
    protected $fillable=['companyName','description','id_image'];

    public function products(){
        return $this->hasMany('App\Product','id','id_company');
    }
    public function image(){
        return $this->belongsTo('App\Image','id_image');
    }
}

Product.php

class Product extends Model
{
    //
    protected $table='products';
    protected $primaryKey='id';
    protected $fillable=[
            'product','video','id_company','id_image',
    ];
    public function Image(){
        return $this->belongsTo('App\Image');
    }
    public function Agent_Company(){
        return $this->belongsTo('App\Agent_Company','id_company','id');
    }
}

有风景:

<div class="container">
    {{$agent_Company->links()}}
    <div class="row" id="company">
        @foreach($agent_Company as $item)
            <div class="card col-md-4" style="width: 300px; max-height: 500px;">
                <div class="card-header">
                    <h2>{{$item->companyName}}</h2>
                </div>
                <img src="{{'agent_Companye_Images/'.$item->image->image}}" class="card-img-top"
                     alt="{{$item->image->alt}}" style="width: 300px; height: 300px">
                <div class="card-body" style="overflow:auto">
                    <h5 class="card-title">Product:{{DD($item->Products->count())}}</h5>
                    <span class="card-title">
                        <a href="" class=" btn btn-primary"> Go to Company</a>
                        {{$item->description}}
                    </span>
                </div>
            </div>
        @endforeach
    </div>
</div>

【问题讨论】:

  • 如果您不急于加载您的products 关系,那么计数将是0,因为您计算的是加载 产品关系的数量,而不是产品关系的数量(存储在数据库中)。
  • 所以..?我应该改变什么?和谢谢
  • 在您的控制器中,急切地加载您的产品关系。例如:AgentCompany::with('products')-&gt;findOrFail(1)

标签: php laravel eloquent relationship eager-loading


【解决方案1】:

@Augusto Moura 是正确的,您已经切换了外键和本地键。你应该先解决这个问题。

要回答您的问题,无需急切加载产品,因为您只需要计数。在处理大量数据时,您会遇到运行时问题(内存)。

因此在你的模型中你应该声明

public function products(){
    return $this->hasMany('App\Product','id_company');
}

由于函数名称区分大小写,因此将产品计为$item-&gt;products()-&gt;count()。如果您要求$item-&gt;products-&gt;count(),laravel 仍会将所有产品加载到内存中并从那里计算它们。利用 ORM model() 关系来避免这种情况。

在数据库级别,区别在于 SQL 请求。 $item->products->count() 类似于 SELECT * from products WHERE ...,其中 laravel 计算对象的数量,而 SELECT count() as products_count from products WHERE ...

您实际上可以通过添加 -&gt;withCount('products') 和 Blade {{ $item-&gt;products_count }} 来急切加载仅计数的产品

【讨论】:

    【解决方案2】:

    您已在模型Agent_Company 中切换products 关系中的参数。正确的是:

    public function products(){
       return $this->hasMany('App\Product','id_company','id');
    }
    

    另外值得注意的是: 我同意@fubar 在几乎所有情况下都需要使用with()eager loading。也就是说,即使您不急于加载,您仍然可以将关系作为属性获取。那就是lazy loading

    【讨论】:

      猜你喜欢
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 2015-12-03
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      相关资源
      最近更新 更多