<?php

namespace App\Http\Controllers;

class ParkingController
{
    public function getAll()
    {
        return Response::json('success', $this->table()->get());
    }

    public function get($id)
    {
        $parking = $this->table()->where('id', $id)->get();

        return Response::json('success', $parking);
    }

    public function rentSpace()
    {
        // Add server side validation check if space is rented
        $id = input('id');

        $parking = $this->getParking($id)->first();

        if ($parking->rented === 'true') {
            return Response::json('fail', [
                'message' => 'The parking space has been rented'
            ]);
        }

        $this->getParking($id)->update(['rented' => 'true']);

        return Response::json('success');
    }

    private function table()
    {
        return \Builder::table('parking');
    }

    private function getParking($id)
    {
        return $this->table()->where('id', $id);
    }
}

<?php

namespace App\Http\Controllers;

class ParkingController
{
    public function getAll()
    {
        return Response::json('success', $this->table()->get());
    }

    public function get($id)
    {
        $parking = $this->table()->where('id', $id)->get();

        if (empty($parking)) {
            return Response::json('fail', [
                'message' => 'The parking space does not exist'
            ]);
        }

        return Response::json('success', $parking);
    }

    public function rentSpace()
    {
        // Add server side validation check if space is rented
        $id = input('id');

        $parking = $this->getParking($id)->first();

        if ($parking->rented === 'true') {
            return Response::json('fail', [
                'message' => 'The parking space has been rented'
            ]);
        }

        $this->getParking($id)->update(['rented' => 'true']);

        return Response::json('success');
    }

    private function table()
    {
        return \Builder::table('parking');
    }

    private function getParking($id)
    {
        return $this->table()->where('id', $id);
    }
}

Return fail message if parking space do not exist
Return fail message if parking space do not exist
