php - 使用whereHas Eloquent 关系查询

标签 php mysql database laravel eloquent

我有以下表格: 用户

Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('username');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
            $table->integer('role_id')->unsigned();
            $table->foreign('role_id')->references('id')->on('roles');

区域

Schema::create('areas', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->integer('location_id')->unsigned();
            $table->foreign('location_id')->references('id')->on('locations')->onDelete('cascade');
            $table->timestamps();

区域用户

Schema::create('area_user', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users');
            $table->integer('area_id')->unsigned();
            $table->foreign('area_id')->references('id')->on('areas');
        });

建筑物

Schema::create('buildings', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('symbol')->nullable();
            $table->integer('area_id')->unsigned();
            $table->foreign('area_id')->references('id')->on('areas')->onDelete('cascade');
            $table->integer('building_type')->unsigned();
            $table->foreign('building_type')->references('id')->on('building_types');
            $table->timestamps();
        });

用户模型:

public function areas()
    {
        return $this->belongsToMany('App\Area');
    } 

区域模型:

public function users(){
        return $this->belongsToMany('App\User');
    }

建筑模型:

public function area(){
       return $this->belongsTo('App\Area','area_id');
    }

如何在用户模型中编写一个查询,该查询返回通过分配给用户的区域(area_user 表)分配给用户的所有建筑物 我尝试了这个,但它返回一个错误

Building::whereHas('area', function ($q) {
            $q->whereHas('users', function ($q) {
                $q->where('id', auth()->id());
            });
        })->get();

错误:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select * from `buildings` where exists (select * from `areas` where `buildings`.`area_id` = `areas`.`id` and exists (select * from `users` inner join `area_user` on `users`.`id` = `area_user`.`user_id` where `areas`.`id` = `area_user`.`area_id` and `id` = 11))) (View: C:\xampp\htdocs\hse\resources\views\observations\form_observation.blade.php) (View: C:\xampp\htdocs\hse\resources\views\observations\form_observation.blade.php)

最佳答案

您可以通过在 where 子句中指定表来解决歧义:

改变

$q->where('id', auth()->id());

$q->where('users.id', auth()->id());

关于php - 使用whereHas Eloquent 关系查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46868227/

相关文章:

php - PHP 中的 Flood-It 游戏递归函数算法

javascript - jQuery 代码不更新数据库中的字段或更新下拉列表

mysql - 如何以自然的方式将频繁修改的列表存储在数据库中,以便它们可以随时读取?

php - 如何检查 laravel 中是否设置了 cookie?

php - 原则 - 在两个日期之间获得销售

php - 带有 ñ 或重音符号的友好 Url 问题字符串

php - CakePHP 上的链式关系

mysql - 当网络很慢时如何阻止相同的数据进入数据库

database - 不同 Entity Framework 模型的相同连接字符串?

database - 在oracle上导出数据库