鍍金池/ 問答/PHP/ 關(guān)于Eloquent ORM 一對多如何進(jìn)行關(guān)聯(lián)排序的問題 ?

關(guān)于Eloquent ORM 一對多如何進(jìn)行關(guān)聯(lián)排序的問題 ?

有兩張表,一張商品表:shop_goods 表結(jié)構(gòu)如下:

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 '商品標(biāo)題',
  `goods_price` decimal(10,2) NOT NULL COMMENT '售價',
  `goods_total` int(11) NOT NULL COMMENT '商品庫存',
  `goods_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '審核狀態(tài)  0未審核,1審核通過,2審核不通過',
  `goods_state` tinyint(4) NOT NULL DEFAULT '0' COMMENT '商品狀態(tài)  0已上架,1已下架',
  `goods_recycle` tinyint(4) NOT NULL DEFAULT '0' COMMENT '回收站  0正常,1回收站',
  `goods_time` int(11) NOT NULL COMMENT '發(fā)布時間 時間戳',
  PRIMARY KEY (`goods_id`)
) ENGINE=InnoDB AUTO_INCREMENT=77 DEFAULT CHARSET=utf8 COMMENT='商品表';

一張評價表:shop_assess 表結(jié)構(gòu)如下:

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 '評價內(nèi)容',
  `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 '物流服務(wù)評分  1分,2分,3分,4分,5分',
  `assess_service` int(11) NOT NULL COMMENT '服務(wù)態(tài)度評分  1分,2分,3分,4分,5分',
  `assess_state` tinyint(4) NOT NULL COMMENT '評論狀態(tài)  0正常,1隱藏',
  PRIMARY KEY (`assess_id`)
) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=utf8 COMMENT='商品評價表';

商品表中的 goods_id (主鍵) 和評價表中的 assess_gcode 字段進(jìn)行了關(guān)聯(lián)。

現(xiàn)有兩個模型如下:

// 商品模型

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Goods extends Model
{
    protected $table = 'shop_goods';
    protected $primaryKey = 'goods_id';
    public $timestamps = false;
    /* 
     * 獲取評論對應(yīng)的商品
     */
    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');
    }
}

現(xiàn)在我查詢是這樣寫的

public function getDone()
    {   
        $good = Goods::with(['hasManyGood' => function ($query){
            $query->where('assess_state',0);//評論狀態(tài)正常的
        }])
        ->where('goods_status',1)
        ->where('goods_state',0)
        ->where('goods_recycle',0)
        ->get();
    }

請求到的部分?jǐn)?shù)據(jù)如下

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一發(fā)貨的韓國"
        "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一發(fā)貨的韓國"
        "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 ?}
  ]
}

請問我該如何在查詢中添加 統(tǒng)計或者排序 的條件,才能夠統(tǒng)計出商品下的評價數(shù),并根據(jù)評價數(shù)量的多少來對商品進(jìn)行排序呢 ?**

回答
編輯回答
夢若殤

加個withCount然后按照這個count排序即可
參考:https://laravel-china.org/doc...

2017年6月7日 22:37
編輯回答
局外人

用scope 試試

類似于這種

clipboard.png

2018年1月15日 07:18