Laravel雄辩地连接三个表
我开始从事一个小型的Laravel项目,我想加入三个表来获得客户组织,因为一个客户可以在多个组织中 我有三个表:Laravel雄辩地连接三个表,laravel,Laravel,我开始从事一个小型的Laravel项目,我想加入三个表来获得客户组织,因为一个客户可以在多个组织中 我有三个表:用户,组织,客户组织 如何获得用户id等于1的客户组织?例如,使用Elount: 这是我的表格结构: 组织表: public function up() { Schema::create('organizations', function (Blueprint $table) { $table->bigIncrements('id');
用户
,组织
,客户组织
如何获得用户id等于1的客户组织?例如,使用Elount:
这是我的表格结构:
组织表:
public function up()
{
Schema::create('organizations', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('organization_name');
$table->string('phone');
$table->string('fax');
$table->string('address');
$table->string('city');
$table->string('zipcode');
$table->string('website');
$table->string('linkedin');
$table->string('facebook');
$table->string('twitter');
$table->timestamps();
});
}
客户组织:
public function up()
{
Schema::create('client_organizations', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->bigInteger('organization_id')->unsigned();
$table->foreign('organization_id')->references('id')->on('organizations');
$table->timestamps();
});
}
注意:我还有Laravel Auth附带的用户表。您不需要三个表,而您的案例有一个透视表
return DB::table('client_organizations')
->join('organizations', 'organizations.id', '=', 'organization_id')
->where('user_id', 1)
->get([
'organizations.*'
]);
您可以将
DB::table('client_organizations')
替换为client_organizations
table的型号名称。您不需要三个表,而您的案例有一个透视表
return DB::table('client_organizations')
->join('organizations', 'organizations.id', '=', 'organization_id')
->where('user_id', 1)
->get([
'organizations.*'
]);
您可以将DB::table('client\u organizations')
替换为client\u organizations
table的型号名称。Read relationshipRead relations