Saturday, May 20, 2017

Laravel and Eloquent : Filter to get only a sub key of json in response

I have a mysql table (5.7.1) called "inventory" with 2 columns :

  • id {String}
  • inventory {json}

inventory column contains a json as following for example:

{
    id : "212",
    inventory : {
        data : {
            id : "3",
            demo : true,
            access : false
        }
    }
}

I would like to get in response the following json :

data : {
    id : "3",
    demo : true,
    access : false
}

For the moment, i use the following method :

$inventory = Inventory::where('id', "212")->first();
return response()->json($inventory);

This code allows to get the entire content of the entry in table which have id = 212.

But how can i do to only get the content of a subKey of json of "inventory" column in response by making a single request to database ?



via wawanopoulos

Advertisement