So I have been battling with a scope in Laravel recently and I am getting the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'visitors.visitorable_id' in 'on clause' (SQL: select
profiles.*,profile_genres.genre_idaspivot_genre_id,profile_genres.profile_idaspivot_profile_idfromprofilesinner joinprofile_genresonprofiles.id=profile_genres.profile_idleft join (SELECT COUNT(id) visits, ip_address, visitorable_id, visitorable_type FROMvisitorsGROUP BY ip_address) occurences onvisitors.visitorable_id=db885a18-f0b7-4f55-9d93-743fbb5d9c94andvisitors.visitorable_type= App\Profile whereprofile_genres.genre_id= 038ba398-8998-4cd6-950c-60b4a6c401cc andprofiles.deleted_atis null)
Here is the query that is causing this error:
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;
}
EDIT As requested, my visitors table migration:
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();
});
}
visitorstable's schema ? I think you are referring to wrong column - jaysingkar