Laravel关系多对多通过外键统计相关数据
Laravel relationship many to many count related data by foreign key
我的项目中有 3 个模型:
用户型号代码:
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
protected $guarded = [];
public function professions()
{
return $this->belongsToMany('App\Models\Profession', 'user_professions');
}
}
专业型号代码
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Profession extends Model
{
protected $guarded = [];
public function users()
{
return $this->belongsToMany('App\Models\User', 'user_professions');
}
}
迁移:
$table->id();
$table->string("name");
$table->timestamps();
用户专业型号代码
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class UserProfession extends Model
{
//
}
迁移:
$table->id();
$table->foreignId('user_id')
->constrained()
->onDelete('cascade');
$table->foreignId('profession_id')
->constrained()
->onDelete('cascade');
当我尝试使用此代码时,我按他的名字搜索用户并找到职业名称,然后计算该职业的用户数。
代码:
$query = $request->get("query");
$users = User::where("name", "like", "%".$query."%");
$userIds = $users->get()->pluck("id")->toArray();
$professions = Profession::whereHas("users", function($q) use($userIds) {
$q->whereIn('id', $userIds);
})->get()->toArray();
我收到错误消息:
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity
constraint violation: 1052 Column 'id' in where clause is ambiguous
(SQL: select * from professions where exists (select * from users
inner join user_professions on users.id = user_professions.user_id
where professions.id = user_professions.profession_id and id in (11,
43, 82)))
我的代码哪里有错误,我该如何解决?
有三个 table users
、user_professions
和 professions
都有 id
列。
您需要指定您想要的 table 的 ID:
$professions = Profession::whereHas("users", function($q) use($userIds) {
$q->whereIn('users.id', $userIds); // specify the table name
})->get()->toArray();
我的项目中有 3 个模型:
用户型号代码:
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
protected $guarded = [];
public function professions()
{
return $this->belongsToMany('App\Models\Profession', 'user_professions');
}
}
专业型号代码
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Profession extends Model
{
protected $guarded = [];
public function users()
{
return $this->belongsToMany('App\Models\User', 'user_professions');
}
}
迁移:
$table->id();
$table->string("name");
$table->timestamps();
用户专业型号代码
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class UserProfession extends Model
{
//
}
迁移:
$table->id();
$table->foreignId('user_id')
->constrained()
->onDelete('cascade');
$table->foreignId('profession_id')
->constrained()
->onDelete('cascade');
当我尝试使用此代码时,我按他的名字搜索用户并找到职业名称,然后计算该职业的用户数。
代码:
$query = $request->get("query");
$users = User::where("name", "like", "%".$query."%");
$userIds = $users->get()->pluck("id")->toArray();
$professions = Profession::whereHas("users", function($q) use($userIds) {
$q->whereIn('id', $userIds);
})->get()->toArray();
我收到错误消息:
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select * from professions where exists (select * from users inner join user_professions on users.id = user_professions.user_id where professions.id = user_professions.profession_id and id in (11, 43, 82)))
我的代码哪里有错误,我该如何解决?
有三个 table users
、user_professions
和 professions
都有 id
列。
您需要指定您想要的 table 的 ID:
$professions = Profession::whereHas("users", function($q) use($userIds) {
$q->whereIn('users.id', $userIds); // specify the table name
})->get()->toArray();