laravel-mongodb
laravel-mongodb copied to clipboard
Selecting specfic column in with relation is not working
- Laravel-mongodb Version: 3.8^
- PHP Version: 7.4.28
- Database Driver & Version:
Description:
This is my code
Product::query()->with('sales', function ($query){ $query->select('unit_count', 'total_sales', 'date'); });
Expected behaviour
The Expected behaviour is that I must have all products with their sales ( Product Has Many Sales) and custom columns only on the sales table.
Actual behaviour
It returns an empty sales array when I try to select specific columns only.
@jenssegers
i have the same issue
my code using $request from HTTP Request
**$query = LogAdmin::query();
if ($request->admin_id) {
$query->where('adm_id', $request->admin_id);
}
return $query->get();**
but it will runs if like this return LogAdmin::where('admin_id', 8)->get(); @jenssegers
needs to add reference ID in select
Product::query()->with('sales', function ($query){
$query->select('unit_count', 'total_sales', 'date', 'product_id');
});
Closing as the solution is to retrieve the relationship fields, as mentioned by @BilalTariq01.