When i use pluck with multiple columns i get this:
{"Kreis 1 \/ Altstadt":"City","Kreis 2":"Enge","Kreis 3":"Sihifeld","Kreis 4":"Hard","Kreis 5 \/ Industriequartier":"Escher Wyss","Kreis 6":"Oberstrass","Kreis 7":"Witikon","Kreis 8 \/ Reisbach":"Weinegg","Kreis 9":"Altstetten","Kreis 10":"Wipkingen","Kreis 11":"Seebach","Kreis 12 \/ Schwamendingen":"Hirzenbach"
But i need this?
["Rathaus","Hochschulen","Lindenhof","City","Wollishofen","Leimbach","Enge","Alt-Wiedikon","Friesenberg","Sihifeld","Werd","Langstrasse","Hard","Gewerbechule","Escher Wyss","Unterstrass","Oberstrass","Fluntern","Hottingen","Hirslanden","Witikon","Seefeld","M\u00fchlebach","Weinegg","Albisrieden","Altstetten","H\u00f6ngg","Wipkingen","Affoltern","Oerlikon","Seebach","Saatlen","Schwamendingen-Mitte","Hirzenbach"]
Any suggestion how can i do that? This is my method:
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 ?', [strtolower('%'.$district . '%')])->orWhereRaw('LOWER(region) like ?', [strtolower('%'.$district . '%')])->pluck('region','district');
return response()->json($data);
}
I have created the model scope
More about scopes:
https://laravel.com/docs/5.8/eloquent#query-scopes
https://medium.com/@janaksan_/using-scope-with-laravel-7c80dd6a2c3d
Code:
So now the testing part
BASIC EXAMPLE
ADVANCED EXAMPLE
But it returns the \Illuminate\Support\Collection, so if you need to convert to array
if you need to convert to json
My solution in LARAVEL 5.6:
Hi, I've just had the same problem, where I needed 2 columns combined in 1 select list. My DB has 2 columns for Users: first_name and last_name. I need a select box, with the users full name visible and the id as value. This is how I fixed it, using the pluck() method:
In the User model I created a full name accessor function:
After that, to fill the select list with the full name & corresponding database id as value, I used this code in my controller that returns the view (without showing users that are archived, but you can change the begin of the query if you like, most important are get() and pluck() functions:
Now you can use the $users in your select list!
So first, you GET all the values from DB that you will need, after that you can use any accessor attribute defined for use in your PLUCK method,
as long as all columns needed for the accessor are in the GET ;-)
You should use
select()
withget()
and then later on modify the object as you need.So instead of:
->pluck('region','district');
use:->select('region','district')->get();
pluck()
is advised when you need value of one column only.And as far as possible, you should have your models singular form not plural (Districts) - to follow Laravel nomenclature.
This is an issue I constantly have faced and has led me to create the following solution that can be used on models or arrays.
There is also support for dot syntax that will create a multidimensional array as required.
Register this macro within the
AppServiceProvider
(or any provider of your choice):This can then be used in the following way:
Or additionally, on any models you may have:
It is also possible to reference any relationship too using the dot notation, as well as using the
as [other name]
syntax:Laravel: To pluck multi-columns in the separate arrays use the following code.
Cos that is how pluck works. Instead try this.