This is json object from database
{
"id": 1,
"price_role": [
{"id": 1, price:20, role:"HQ"},
{"id": 2, price:10, role:"AG"}
]
}
My question is how to filter a specific price_role by a given role and convert it to an object instead of returning list?
Try using this way but it is not worked :
$role = "HQ";
$query = Product::with('price_role', function ($q) use ($role) {
$q->where('role', '=', $role);
})
->where('user_id', '=', $user->id)->get();
Expected output if $role = "HQ" :
{
"id": 1,
"price_role": {"id": 1, price:20, role:"HQ"},
}
FYI: I'm using Laravel 5.5
but it is not workingwhat exactly is happening now?