How to use alias column in wherein with laravel?

by lindsey.homenick , in category: PHP Frameworks , a year ago

How to use alias column in wherein with laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by giovanny.lueilwitz , a year ago

@lindsey.homenick 

To use an alias column in a whereIn query with Laravel, you can use the selectRaw method to specify the alias column in the query. Here's an example:

1
2
3
4
5
6
7
$aliasColumn = 'alias_name';
$ids = [1, 2, 3];

$results = DB::table('table_name')
    ->selectRaw('id AS ' . $aliasColumn)
    ->whereIn('id', $ids)
    ->get();


In this example, we are selecting the "id" column from the table, but aliasing it as "alias_name". Then, we use the whereIn method to filter the results based on the array of IDs provided. Finally, we use the get method to retrieve the results.


You can modify the column and table names as needed for your specific use case.

Related Threads:

How to use column from other table in laravel?
How to update only 3 column value in laravel?
How to use dynamic column name in postgresql?
How to use order by in alphanumeric column in oracle?
How to drop column in Laravel migration?
How to remove column in Laravel migration?