关于laravel框架 Eloquent ORM 一对多关联后如何进行排序 ?


有两张表,一张商品表:shop_goods 表结构如下:

CREATE TABLE `shop_goods` (
  `goods_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '商品编号(自增ID)',
  `goods_name` char(60) NOT NULL COMMENT '店铺名称',
  `goods_title` char(60) NOT NULL COMMENT '商品标题',
  `goods_price` decimal(10,2) NOT NULL COMMENT '售价',
  `goods_total` int(11) NOT NULL COMMENT '商品库存',
  `goods_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '审核状态  0未审核,1审核通过,2审核不通过',
  `goods_state` tinyint(4) NOT NULL DEFAULT '0' COMMENT '商品状态  0已上架,1已下架',
  `goods_recycle` tinyint(4) NOT NULL DEFAULT '0' COMMENT '回收站  0正常,1回收站',
  `goods_time` int(11) NOT NULL COMMENT '发布时间 时间戳',
  PRIMARY KEY (`goods_id`)
) ENGINE=InnoDB AUTO_INCREMENT=77 DEFAULT CHARSET=utf8 COMMENT='商品表';

一张评价表:shop_assess 表结构如下:

CREATE TABLE `shop_assess` (
  `assess_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '评价ID',
  `assess_gcode` int(11) NOT NULL COMMENT '商品编号',
  `assess_uid` char(255) NOT NULL COMMENT '用户编号',
  `assess_name` char(20) NOT NULL COMMENT '用户名称',
  `assess_content` text COMMENT '评价内容',
  `assess_img` text COMMENT '评价图片',
  `assess_stime` int(11) NOT NULL COMMENT '评价时间  时间戳',
  `assess_describe` int(11) NOT NULL COMMENT '描述评分   1分,2分,3分,4分,5分',
  `assess_express` int(11) NOT NULL COMMENT '物流服务评分  1分,2分,3分,4分,5分',
  `assess_service` int(11) NOT NULL COMMENT '服务态度评分  1分,2分,3分,4分,5分',
  `assess_state` tinyint(4) NOT NULL COMMENT '评论状态  0正常,1隐藏',
  PRIMARY KEY (`assess_id`)
) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=utf8 COMMENT='商品评价表';

商品表中的 goods_id (主键) 和评价表中的 assess_gcode 字段进行了关联。
现有两个模型如下:

// 商品模型
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Goods extends Model
{
protected $table = 'shop_goods';
protected $primaryKey = 'goods_id';
public $timestamps = false;
/* 
* 获取评论对应的商品
*/
public function hasManyGood()
{
return $this->hasMany('App\Models\Assess','assess_gcode','goods_id');
}
}
// 评价模型
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Assess extends Model
{
    protected $table = 'shop_assess';
    protected $primaryKey = 'assess_id';
    public $timestamps = false;
    /* 
     * 获取商品的所有评价
     */
    public function hasManyAssess()
    {
        return $this->belongsTo('App\Models\Goods','assess_gcode','goods_id');
    }
}

现在我查询是这样写的

public function getDone()
    {   
        $good = Goods::with(['hasManyGood' => function ($query){
            $query->where('assess_state',0);//评论状态正常的
        }])
        ->where('goods_status',1)
        ->where('goods_state',0)
        ->where('goods_recycle',0)
        ->get();
    }

请求到的部分数据如下

Collection {#2161 ▼
  #items: array:5 [▼
    0 => Goods {#2111 ▼
      #table: "shop_goods"
      #primaryKey: "goods_id"
      +timestamps: false
      #connection: null
      #perPage: 15
      +incrementing: true
      #attributes: array:45 [▼
        "goods_id" => 51
        "goods_scode" => "1"
        "goods_name" => "3一发货的韩国"
        "goods_publisher" => "ning123456"
        "goods_title" => "华为手机"
        "goods_price" => "2000.00"
        "goods_status" => 1
        "goods_state" => 0
        "goods_recycle" => 0
        "goods_time" => 1527644435
      ]
      #original: array:45 [▼
        "goods_id" => 51
        "goods_scode" => "1"
        "goods_name" => "3一发货的韩国"
        "goods_publisher" => "ning123456"
        "goods_title" => "华为手机"
        "goods_price" => "2000.00"
        "goods_status" => 1
        "goods_state" => 0
        "goods_recycle" => 0
        "goods_time" => 1527644435
      ]
      #relations: array:1 [▼
        "hasManyGood" => Collection {#2110 …1}
      ]
      #hidden: []
      #visible: []
      #appends: []
      #fillable: []
      #guarded: array:1 [▼
        0 => "*"
      ]
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
    1 => Goods {#2112 ?}
    2 => Goods {#2113 ?}
    3 => Goods {#2114 ?}
    4 => Goods {#2115 ?}
  ]
}

请问我该如何在查询中添加 统计或者排序 的条件,才能够统计出商品下的评价数,并根据评价数量的多少来对商品进行排序呢 ?


Vote Vote Cancel Collect Collect Cancel

<< 上一篇: 关于路由不存在和csrf_field没有填写的报错问题

>> 下一篇: laravel-admin 如何自定义一个自增列?