Damjan Krstevski
Damjan Krstevski

Reputation: 265

Laravel 5, orderByRaw using field and column doesn't exists

I have problems with Eloquent/QueryBuilder when I use orderByRaw function I got exception column doesnt exists, but the column is already exists into database.

Here is the exception:

SQLSTATE[42883]: Undefined function: 7 ERROR:  function field(integer, integer) does not exist
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

The status column into database is integer. Here is my code:

$orderedStatuses = implode(',', [4]);
$users->orderByRaw(\DB::raw("FIELD(status, $orderedStatuses)"));

...also, if I use users.status I got the same error.

Can someone help me?

Thanks

Upvotes: 2

Views: 7223

Answers (2)

Sandeesh
Sandeesh

Reputation: 11916

You mixed both the methods.

$users->orderBy(\DB::raw("FIELD(status, $orderedStatuses)"));

Or

$users->orderByRaw("FIELD(status, $orderedStatuses)");

Upvotes: 1

aceraven777
aceraven777

Reputation: 4556

orderByRaw expects raw SQL, you have to pass string in the argument of orderByRaw.

Example:

$users->orderByRaw('field1 ASC, field2 DESC, field3 DESC,...');

Upvotes: 0

Related Questions