LaraSell
4 months ago
403
2
General

json_encode not work in custom output

Posted 4 months ago by LaraSell

I need too show query in json format:

class LocationController extends Controller { public function index(){

        $zoneNameByCountry = DB::table('zone')->where('country_id', '101')->first();

        echo json_encode(['output'=>$zoneNameByCountry]);
    }
}

Now in ouput i see:

{"output":{"zone_id":1538,"country_id":101,"name":"London","code":"30","status":1,"created_at":"2020-02-11 10:46:45","updated_at":"2020-02-11 10:46:45"}}

But I need to this:

{"output":[{"zone_id":1538,"country_id":101,"name":"London","code":"30","status":1,"created_at":"2020-02-11 10:46:45","updated_at":"2020-02-11 10:46:45"}]}

In My first output(false) json_encode remove [ and ]!! How do can i fix this problem?

Please sign in or create an account to participate in this conversation.