如何获得 parent_id 计数?

How to get parent_id count?

用户table

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('first_name');
        $table->string('last_name');
        $table->string('referral_code')->nullable();
        $table->integer('parent_id')->unsigned()->nullable();
        $table->string('mobile')->unique();
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

订单table

public function up()
{
    Schema::create('oreders', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->bigInteger('product_id')->unsigned();
        $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
        $table->timestamps();
    });
}

我试过 first 并且 whereHas('user') 替换了它

$orderCount = Order::whereHas('user')->withCount('parent_id')->get();
return $orderCount;

我收到这个错误。

Call to undefined method App\Order::parent_id() (View: C:\xampp\htdocs\site\bazar\resources\views\Admin\master.blade.php)

您首先需要在模型 App\UserApp\Order

中定义关系

App/User.php

class User extends Model
{

    public function orders()
    {
        return $this->hasMany(Order::class);
    }

    public function parent()
    {
        return $this->belongsTo(User::class, 'parent_id');
    }

    public function children()
    {
        return $this->hasMany(User::class, 'parent_id');
    }

}

App/Order.php

class Order extends Model
{

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

我相信你想统计一个用户的订单数量。
但首先,我们会解决一些问题/我怀疑你有。

  • Table名字orders被称为oreders
  • 您无需验证订单是否有用户 Order::whereHas('user'),因为 $table->bigInteger('user_id')->unsigned(); 不可为空。这意味着没有用户就不能存在订单。
  • 不是问题,但建议 $table->bigInteger('user_id')->unsigned(); 可以简化为 $table->unsignedBigInteger('user_id');

现在是有趣的部分

$orderCount = Order::whereHas('user')->withCount('parent_id')->get();
return $orderCount;

据我了解,您正在尝试获取 parent 用户的订单数量。我将向您展示一些可能有助于您理解的用例。

// Get the total number of orders
$orderCount = Order::count();

// Get the total number of orders of a user
$userOrderCount = $user->orders()->count();

// Include the number of orders in the user attributes
$user = User::withCount('orders')->find($userId); // notice 'order' is, in fact `orders()` from the App\User methods

// Include the number of orders in the parent attributes
$parent = User::withCount('orders')->find($user->parent_id);

// Number of orders of the parent
$parentOrderCount = Order::where('user_id', $user->parent_id)->count();

// Edit: As you commented, you also want to know "The marketers can see how many people bought their code"
// I'm assuming this is the number of children (I have added the relation in the model above)
$childrenCount = $user->children()->count()

注意:当您执行 Order::where('user_id', $user->parent_id)->count(); 时,您不需要先验证用户是否有 parent。 parent_id 将 return 为空,user_id 不能为 null。所以它只会 return 0