On the question of how to sort the association of Eloquent ORM one-to-many?

< H1 > there are two tables, one merchandise table: shop_goods table structure is as follows: < / H1 >
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,12",
  `goods_state` tinyint(4) NOT NULL DEFAULT "0" COMMENT "  0,1",
  `goods_recycle` tinyint(4) NOT NULL DEFAULT "0" COMMENT "  01",
  `goods_time` int(11) NOT NULL COMMENT " ",
  PRIMARY KEY (`goods_id`)
) ENGINE=InnoDB AUTO_INCREMENT=77 DEFAULT CHARSET=utf8 COMMENT="";
< H1 > an evaluation table: shop_assess the structure of the table is as follows: < / H1 >
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 "  01",
  PRIMARY KEY (`assess_id`)
) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=utf8 COMMENT="";

The goods_id (primary key) in the < H1 > merchandise table is associated with the assess_gcode field in the evaluation table. < / H1 > < H1 > the two existing models are as follows: < / H1 >

/ / Commodity Model

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");
    }
}

/ / Evaluation model

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");
    }
}

< H1 > now my query is written like this < / H1 >
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();
    }
< H1 > part of the data requested is as follows < / H1 >
Collection {-sharp2161 
  -sharpitems: array:5 [
    0 => Goods {-sharp2111 
      -sharptable: "shop_goods"
      -sharpprimaryKey: "goods_id"
      +timestamps: false
      -sharpconnection: null
      -sharpperPage: 15
      +incrementing: true
      -sharpattributes: 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
      ]
      -sharporiginal: 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
      ]
      -sharprelations: array:1 [
        "hasManyGood" => Collection {-sharp2110 1}
      ]
      -sharphidden: []
      -sharpvisible: []
      -sharpappends: []
      -sharpfillable: []
      -sharpguarded: array:1 [
        0 => "*"
      ]
      -sharpdates: []
      -sharpdateFormat: null
      -sharpcasts: []
      -sharptouches: []
      -sharpobservables: []
      -sharpwith: []
      -sharpmorphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
    1 => Goods {-sharp2112 }
    2 => Goods {-sharp2113 }
    3 => Goods {-sharp2114 }
    4 => Goods {-sharp2115 }
  ]
}
< H1 > how can I add statistics or sorting conditions to the query so that I can count the number of evaluations under the goods and sort the goods according to the number of evaluations? * * < / H1 >
Mar.17,2021

try scope

similar to this

clipboard.png


add a withCount and sort by this count
reference: https://laravel-china.org/doc.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b375d5-2b805.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b375d5-2b805.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?