【发布时间】:2019-08-07 06:55:40
【问题描述】:
我有两个名为users 和buys 的表:
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('buys', 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();
});
}
我想加入 users.parent_id 和 buys.user_id。这是我当前的查询:
public function user ()
{
return $this->hasOne(User::class);
}
我的查询:
$users = Buy::all()->where('parent_id', auth()->user()->id)->latest()->paginate(25);
但是我的查询抛出了这个:
SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列 'parent_id'(SQL:从购买 where parent_id = 2 中选择 count(*) 作为聚合)
知道如何解决吗?
【问题讨论】:
-
您的查询代码在哪里?您刚刚添加了关系。
-
您放置用户方法的位置
-
你想和
users.parent_id一起加入buys.user_id吗? -
@zahidhasanemon 我的查询已添加。