1
votes

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_id as pivot_genre_id, profile_genres.profile_id as pivot_profile_id from profiles inner join profile_genres on profiles.id = profile_genres.profile_id left join (SELECT COUNT(id) visits, ip_address, visitorable_id, visitorable_type FROM visitors GROUP BY ip_address) occurences on visitors.visitorable_id = db885a18-f0b7-4f55-9d93-743fbb5d9c94 and visitors.visitorable_type = App\Profile where profile_genres.genre_id = 038ba398-8998-4cd6-950c-60b4a6c401cc and profiles.deleted_at is 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();
        });
    }
2
Unknown column 'visitors.visitorable_id'. Does visitorable_id exist on the table? - mbozwood
can you post your visitors table's schema ? I think you are referring to wrong column - jaysingkar
Updated the question with my migration - ChrisBratherton

2 Answers

1
votes
SELECT
    profiles.*, 
    profile_genres.genre_id as pivot_genre_id,
    profile_genres.profile_id as pivot_profile_id
FROM
    profiles
    INNER JOIN profile_genres ON profiles.id = profile_genres.profile_id
    LEFT JOIN (
        SELECT
            COUNT(id)
            visits,
            ip_address,
            visitorable_id,
            visitorable_type
        FROM
            visitors
        GROUP BY
            ip_address
    ) occurences ON
        visitors.visitorable_id = XXX AND
        visitors.visitorable_type = App\Profile)
WHERE
    profile_genres.genre_id = YYY AND
    profiles.deleted_at IS null

Basically, you do a select statement from visitors, but the response from the select statement you call occurences. Thus visitors.visitorable_id does NOT exist in the temp table, but occurences.visitorable_id should exist.

0
votes

So it turns out that the visitors table wasn't available within the sub query, changing visitors.visitorable_id to occurences.visitorable_id did the trick.

$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('occurences.visitorable_id', '=', 'profiles.id');
    $join->where('occurences.visitorable_type', '=', 'App\Profile');
});