Bestellanfrage von Unterabfrage. Eloquent vs MySql

Dennis Muntjans:

Ich möchte Listen erhalten , die von einem bestimmten Benutzer und Bestelllisten von gefolgt wird created_at(nach Datum , wenn Listen gefolgt wurden). Nicht sicher , warum es nicht funktioniert (Reihenfolge nach) mit Eloquent:

$myQuery = PlaceList::query()
   ->where('private', 0)
   ->whereHas('followers', function ($query) use ($userID) {
      $query->where('created_by_user_id', $userID);
   })
   ->orderByDesc(
      Follow::query()->select('created_at')
         ->where('created_by_user_id', $userID)
         ->where('followable_id', 'place_lists.id')
   )
   ->get()

Das ist , was ich habe , wenn ich verwende dd($myQuery->toSql()):

select 
    * 
from 
    `place_lists` 
where 
    `private` = ? 
    and exists (
                select 
                   * 
                from 
                   `follows` 
                where 
                   `place_lists`.`id` = `follows`.`followable_id` 
                   and `created_by_user_id` = ? 
                   and `follows`.`deleted_at` is null
                ) 
    and `place_lists`.`deleted_at` is null 
order by (
   select 
      `created_at` 
   from 
      `follows` 
   where 
      `created_by_user_id` = ? 
      and `followable_id` = ? 
      and `follows`.`deleted_at` is null
) desc

Aber wenn ich bevölkern MySql mit Daten funktioniert es:

select 
    * 
from 
    `place_lists` 
where 
    `private` = 0 
    and exists (
                select 
                   * 
                from 
                   `follows` 
                where 
                   `place_lists`.`id` = `follows`.`followable_id` 
                   and `created_by_user_id` = 13 
                   and `follows`.`deleted_at` is null
                ) 
    and `place_lists`.`deleted_at` is null 
order by (
    select 
        `created_at` 
    from 
        `follows` 
    where 
       `created_by_user_id` = 13 
       and `followable_id` = `place_lists`.`id`
       and `follows`.`deleted_at` is null
) desc

Ich bin auch verwirrt, was ich falsch mache?

mrhn:

Diese Abfrage, behaupten Sie , dass followalbe_idgleich 'place_lists.id'.

->orderByDesc(
    Follow::query()->select('created_at')
        ->where('created_by_user_id', $userID)
        ->where('followable_id', 'place_lists.id')
)

So vergleichen Sie zwei Spalten verwenden whereColumn()statt.

->orderByDesc(
    Follow::query()->select('created_at')
        ->where('created_by_user_id', $userID)
        ->whereColumn('followable_id', 'place_lists.id')
)

Ich denke du magst

Origin http://43.154.161.224:23101/article/api/json?id=373276&siteId=1
Empfohlen
Rangfolge