【发布时间】:2018-02-04 13:07:14
【问题描述】:
所以我最近一直在与 Laravel 中的一个范围作斗争,我收到以下错误:
SQLSTATE[42S22]:未找到列:1054 未知列 'on 子句'中的'visitors.visitorable_id'(SQL:选择
profiles.*,profile_genres.genre_id作为pivot_genre_id,profile_genres.profile_id作为pivot_profile_id来自profiles内部连接profile_genresprofiles.id=profile_genres.profile_idleft join (SELECT COUNT(id) 访问, ip_address、visitorable_id、visitorable_type FROMvisitorsGROUP BY ip_address) 出现在visitors.visitorable_id=db885a18-f0b7-4f55-9d93-743fbb5d9c94和visitors.visitorable_type= App\Profile 在哪里profile_genres.genre_id= 038ba398-8998-4cd6-950c-60b4a6c401cc 和profiles.deleted_at为空)
这是导致此错误的查询:
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?
-
您能发布您的
visitors表的架构吗?我认为您指的是错误的列 -
用我的迁移更新了问题
标签: php mysql laravel eloquent query-builder