Laravel 5.2 - Pluck() Method Returns Array

Laravel 5.2 - pluck() method returns array

The current alternative for pluck() is value().

How to return an array instead of a collection in Laravel?

Just use pluck() and ->toArray():

Foo::where('b', 15)->pluck('a')->toArray();

Laravel's pluck() method.

Laravel's toArray() method.

Laravel 5.2 pluck() multiple attributes from Eloquent Model Collection

Laravel: 5.7

if method is returning relation

use get()

e.g

$lesson = Lesson::find(1);
$lesson->users()->get(['id', 'name']);

on collections

use only()

$user = collect(['id' => 1, 'name' => 'Foo', 'role' => 'A']);
$user->only(['id', 'name']);

multiple arrays

$users = collect([
['id' => 1, 'name' => 'Foo', 'role' => 'A'],
['id' => 2, 'name' => 'Bar', 'role' => 'B'];
]);

$result = $users->map(function($user){
return Arr::only($user, ['id', 'name']);
});

var_dump($result);

Bool is returning an array causing true response every time in Laravel 5.2

Both get() and pluck() will return a collection, so making an if condition like the one you are doing - will not return false even if a collection is completely empty (collection method isEmpty() would return false though). The result of if ($active) in your code has nothing to do with the value of the 'active' field itself.

You can try adding first() to the chain, assuming that you only have or you only need one item:

$booking = guests::where('booking', '=', $term)->get()->first();
$active = guests::where('booking', '=', $term)->pluck('active')->first();

laravel Pluck() returns a single row not collection

In controller

//should just make a model for sections then you could just do:
// return Section::select('name', 'id')->where("division_id",$request->division)->get();

//controller will output it as json by default
return DB::table("sections")->select('name', 'id')->where("division_id",$request->division)->get();

view:

//you dont need a key value object just loop over results and reference it by property as shown below

<script type="text/javascript">
$('#division').change(function(){
var divisionID = $(this).val();
if(divisionID){
$.ajax({
type:"GET",
dataType: "json",
url:"{{url('get-section-list')}}?division="+divisionID,
success:function(res){
if(res){

$("#section").empty();
$("#section").append('<option>Select</option>');
$.each(res,function(placement,row){
$("#section").append('<option value="'+row.id+'">'+row.name+'</option>');
});

}else{
$("#section").empty();
}
}
});
}else{
$("#section").empty();

}
});
</script>

Pluck with multiple columns?

Cos that is how pluck works. Instead try this.

$data = Districts::whereRaw('LOWER(district) like ?', [strtolower('%'.$district . '%')])->orWhereRaw('LOWER(region) like ?', [strtolower('%'.$district . '%')])->select('region', 'district')->get();

$data = collect($data->toArray())->flatten()->all();

Laravel collection pluck dropping a value

The issue is with pluck('ctry', 'val'). This will return val as key & ctry as value. In your query output at & br has same value 1. So one of it getting replaced by the other one.

Try pluck('val', 'ctry')->map(function($value, $country)

Reference



Related Topics



Leave a reply



Submit