所以我一直在Laravel一個範圍最近作戰,我收到以下錯誤:SQLSTATE [42S22]:列未找到:1054未知列(似乎試圖使用ID作爲列名)
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'visitors.visitorable_id' in 'on clause' (SQL: select
profiles
.*,profile_genres
.genre_id
aspivot_genre_id
,profile_genres
.profile_id
aspivot_profile_id
fromprofiles
inner joinprofile_genres
onprofiles
.id
=profile_genres
.profile_id
left join (SELECT COUNT(id) visits, ip_address, visitorable_id, visitorable_type FROMvisitors
GROUP BY ip_address) occurences onvisitors
.visitorable_id
=db885a18-f0b7-4f55-9d93-743fbb5d9c94
andvisitors
.visitorable_type
= App\Profile whereprofile_genres
.genre_id
= 038ba398-8998-4cd6-950c-60b4a6c401cc andprofiles
.deleted_at
is null)
下面是導致此錯誤的查詢:
public function scopePopular($query, $limit = 10)
{
$query->leftJoin(DB::raw('(SELECT COUNT(id) visits, ip_address, visitorable_id, visitorable_type FROM `visitors` GROUP BY ip_address) occurences'), function($join)
{
$join->on('visitors.visitorable_id', '=', 'db885a18-f0b7-4f55-9d93-743fbb5d9c94');
$join->where('visitors.visitorable_type', '=', 'App\Profile');
});
return $query;
}
編輯 按照要求,我的訪客表遷移:
public function up()
{
Schema::create('visitors', function(Blueprint $table) {
$table->increments('id');
$table->uuid('visitorable_id');
$table->string('visitorable_type');
$table->string('isp')->nullable();
$table->string('country_code')->nullable();
$table->string('country')->nullable();
$table->string('city')->nullable();
$table->string('region')->nullable();
$table->string('region_name')->nullable();
$table->string('ip_address')->nullable();
$table->string('timezone')->nullable();
$table->string('zip_code')->nullable();
$table->string('latitude')->nullable();
$table->string('longitude')->nullable();
$table->timestamps();
});
}
未知列'visitors.visitorable_id'。桌上是否存在visitorable_id? – mbozwood
你可以發佈你的'visitor'表的模式嗎?我認爲你指的是錯誤的列 – jaysingkar
用我的遷移更新了問題 – ChrisBratherton