Pluck with multiple columns?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Mastering Pluck with Multiple Columns in Laravel Applications
Introduction
One of the powerful features of Laravel is its eloquent ORM that makes working with data easy and efficient. The pluck method enables you to return only the specific column values from a result set, making it convenient for retrieving required information from databases. However, when dealing with multiple columns, things can get a bit tricky. This blog post aims at helping you understand how to use pluck effectively with multiple columns and provide suggestions to achieve your desired output.
Suggested Approach
1. Analyze the current method: In the code snippet provided, it seems that you are fetching data from the 'districts' table and filtering based on a query string. However, it would be helpful if you could also return an array with both regions and districts for each row, instead of only one column.
2. Make changes to the method: To achieve this, modify your code as follows:
- Add another array named 'districts' in the response data structure.
- Add a new SQL query that selects both the 'region' and 'district' columns.
- Replace the two existing conditionals with one using a combination of 'OR'.
- Use pluck on this new result to get the required columns.
Updated Code Snippet:
public function autocomplete_districts(Request $request) {
$district = $request->input('query');
// $ass = DB::table('districts')->select(array('district', 'region'))->get();
// dd($ass);
$data = Districts::whereRaw('LOWER(district) like ? or LOWER(region) like ?', [strtolower('%'.$district . '%'), strtolower('%'.$district . '%')])->pluck(['region','district']);
// Create new array for districts with corresponding regions
$districts = [];
foreach ($data as $row) {
$subArray = [
'district' => $row['district'],
'region' => $row['region']
];
$districts[] = $subArray;
}
return response()->json($districts);
}
Analysis
By following the above approach, you can provide a more comprehensive list of districts with their respective regions in your autocomplete_districts method. This will give users an easier way to find the district they are looking for while also seeing its corresponding region. Remember that Laravel's pluck function is highly versatile and can handle multiple columns, making it essential to understand its usage thoroughly.
Conclusion
In this blog post, we have explored how to use Laravel's pluck method with multiple columns effectively. By modifying your current code, you can achieve the desired output and provide a better user experience in your autocomplete_districts function. It is essential to understand the power of Eloquent ORM and its methods in Laravel applications for efficient database handling and data retrieval.