@ponnydalen you can use pluck function like this
use Illuminate\Support\Arr;
$array = [
['developer' => ['id' => 1, 'name' => 'Taylor']],
['developer' => ['id' => 2, 'name' => 'Abigail']],
];
$names = Arr::pluck($array, 'developer.name');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello. I have 3 checkboxes
<div class="mx-auto">
@foreach($services as $service)
<div class="row">
<div class="form-check">
<label>{{$service->name}}</label>
<input class="service_type" type="checkbox" name="service_type[]" value="{{ $service->service_type }}" @if (in_array($service->service_type, $booking->service_type)) checked="checked" @endif>
</div>
</div>
@endforeach
</div>
I am using them with ajax so I can return the price from db when I click on them. In ajax I get the values like this:
var service_type = $("input[name^='service_type']:checked").map(function (idx, ele) {
return $(ele).val();
}).get();
alert(service_type);
And I get the values (1,2,3) matching the ID if I click on them, and so far so good.
In Controller
$booking = Booking::find($id); // Get user ID
$service_type = $request->service_type; // Get values from "form"
$service_type = Service::where('service_type', $service_type)->get();
return response()->json([
'service-price' => $service_type->price, // Here I want to return the price column for the matching service
]);
"Property [price] does not exist on this collection instance."
So If $service_type contains ["1","2","3"] if everything is checked, and I try to match it with the service type column in service table, why dosent that work?
@ponnydalen Change it to this.
$serviceTotal = Service::whereIn('id', $service_id ?: [])->sum('price') ?? 0;
And mark this thread as solved. Thanks.
Please or to participate in this conversation.