2017-08-26 40 views
1

所以我一直在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 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)

下面是導致此錯誤的查詢:

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(); 
     }); 
    } 
+0

未知列'visitors.visitorable_id'。桌上是否存在visitorable_id? – mbozwood

+0

你可以發佈你的'visitor'表的模式嗎?我認爲你指的是錯誤的列 – jaysingkar

+0

用我的遷移更新了問題 – ChrisBratherton

回答

0

所以事實證明visitors表在子查詢中不可用,將visitors.visitorable_id更改爲occurences.visitorable_id的確有竅門。

$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'); 
}); 
1
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 

基本上,你做的參觀者select語句,但是從select語句的響應調用occurences。因此visitors.visitorable_id不存在於臨時表中,但occurences.visitorable_id應該存在。

相關問題