Eloquent orderByRaw error with two withCount() value on PGSQL #45479
-
-Likes table migration: public function up()
{
Schema::create('likes', function (Blueprint $table) {
$table->id();
$table->bigInteger('user_id')->unsigned();
$table->boolean('direction');
$table->morphs('likeable');
$table->timestamps();
});
Schema::table('likes', function ($table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
} Like model: public function likeable()
{
return $this->morphTo();
} In Entry model: public function likes(): morphMany
{
return $this->morphMany(Like::class, 'likeable')->where('direction', true);
}
public function dislikes(): morphMany
{
return $this->morphMany(Like::class, 'likeable')->where('direction', false);
}
public function likesAndDislikes(): morphMany
{
return $this->morphMany(Like::class, 'likeable');
} I run this query: Entry::withCount(['likes', 'dislikes'])
->orderByRaw('(likes_count + (dislikes_count * -1)) desc')
->paginate(); I got error:
How can I order the model by this formula (likes_count + (dislikes_count * -1) Note: this query works on mysql. But not on pgsql. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
Update: |
Beta Was this translation helpful? Give feedback.
-
这是来自QQ邮箱的自动回复邮件。
您好,来信已收到,谢谢。祝您生活愉快。
|
Beta Was this translation helpful? Give feedback.
-
Hello @staudenmeir can you fix this for pgsql builder/grammar for eloquent? |
Beta Was this translation helpful? Give feedback.
-
@denizgolbas Laravel can't handle this kind of query for PostgreSQL users automatically.
You need to build this query yourself with Eloquent (or the query builder) using |
Beta Was this translation helpful? Give feedback.
@denizgolbas Laravel can't handle this kind of query for PostgreSQL users automatically.
You need to build this query yourself with Eloquent (or the query builder) using
fromSub()
.