如何获得parent_id计数?

时间:2019-08-09 08:41:32

标签: laravel-5.8

用户表

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

订单表

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

我尝试过firstwhereHas('user')取代了它

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

我收到此错误。

  

调用未定义的方法App \ Order :: parent_id()(查看:C:\ xampp \ htdocs \ site \ bazar \ resources \ views \ Admin \ master.blade.php)

1 个答案:

答案 0 :(得分:2)

您首先需要在模型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);
    }
}

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

  • 表名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;

据我了解,您正在尝试获取该用户父级的订单数量。我将向您展示一些可以帮助您理解的用例。

// 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_id将返回null,而user_id不能为null。因此它将只返回0