I am trying to get array of related model to my data and it returns null.
Code
return Product::with(['allBarcodes' => function ($query) {
$query->select('serial_number');
}])->get();
result
Also I tried pluck like $query->pluck('serial_number'); and result was
My real data
the data I suppose to receive is like
[{
"id":1,
"product_id":1,
"serial_number":"5245412185", // I only need this to be return as array
"sold":1,
"created_at":"2020-05-24T04:21:56.000000Z",
"updated_at":"2020-05-24T04:21:56.000000Z"
}]
When I was doing this $query->select('serial_number'); I was only selecting serial_number and not the column that connects both the modals i.e. product_id inside barcodes table.
$query->select('product_id', 'serial_number');. However this will return 2 columns. If somebody wants just one then he/she will have to use [collection transform][1].
All fair options. Also, I know it’s not sexy, but it’s worth remembering you can do DB statements and simply do your joins, etc. I think as Laravel devs we get a bit too attached with Eloquent.