irankhosravi
2 months ago
171
8
Laravel

How to find parent_id count?

Posted 2 months ago by irankhosravi

How to find parent_id count?

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

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

}

My Controller.php

public function index()
{
$orders = Order::with('user')->where(''parent_id)->count();
    return $orders;
}

I get this error.

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'parent_id' in 'where clause' (SQL: select count(*) as aggregate from orders where exists (select * from users where orders.user_id = users.id) and parent_id is null)

Please sign in or create an account to participate in this conversation.