Laravel 通过中间查找记录 table

Laravel find records through intermediate table

我有 3 个模型:

一个用户可以拥有多个公司,一个公司只能属于一个用户。一个查询可以属于多个公司,一个公司可以有多个查询。

迁移如下所示:

用户迁移

Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('first_name');
            $table->string('last_name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('contact_number');
            $table->longText('address')->nullable();
            $table->integer('postal_code');
            $table->string('activation_token')->nullable();
            $table->boolean('active')->default(false);
            $table->enum('type', ['Admin', 'End User', 'Service Provider', 'Broker']);
            $table->string('password')->nullable();
            $table->rememberToken();
            $table->timestamps();
            $table->softDeletes();
        });

公司移民

Schema::create('companies', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('service_provider_id')->nullable();
            $table->unsignedInteger('broker_id');
            $table->string('name');
            $table->string('email')->unique()->nullable();
            $table->string('contact_number');
            $table->longText('address')->nullable();
            $table->integer('postal_code');
            $table->enum('status', ['Confirmed', 'Declined', 'New'])->default('New');
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::table('companies', function (Blueprint $table) {
            $table->foreign('service_provider_id')->references('id')->on('users');
            $table->foreign('broker_id')->references('id')->on('users');
        });

查询迁移

Schema::create('enquiries', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('end_user_id');
            $table->unsignedInteger('category_id');
            $table->string('title');
            $table->longText('description');
            $table->integer('radius');
            $table->enum('status', ['New', 'In Progress', 'Complete'])->default('New');
            $table->timestamps();
        });

        Schema::table('enquiries', function (Blueprint $table) {
            $table->foreign('end_user_id')->references('id')->on('users');
            $table->foreign('category_id')->references('id')->on('categories');
        });

公司查询迁移

Schema::create('company_enquiry', function (Blueprint $table) {
            $table->integer('company_id')->unsigned()->index();
            $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
            $table->integer('enquiry_id')->unsigned()->index();
            $table->foreign('enquiry_id')->references('id')->on('enquiries')->onDelete('cascade');
            $table->primary(['company_id', 'enquiry_id']);
        });

我已经在各自的模型中建立了各种关系。

我想要实现的是查询数据库以仅检索那些通过公司属于用户的查询。

我怎样才能做到这一点?

类似这样(添加真实模型、关系名称和列):

$enquiries = Enquiries::whereHas(['companies' => function($query){
                $query->whereHas(['user' => function($query){
                     $query->where('id', $user_id);
                }]);
            })
            ->get();

您可以使用较短的代码以这种方式完成。

$enquiries = Enquiries::whereHas('companies.user', function($query) use($user_id){
       $query->where('id', $user_id);
 })->get();