phpvalidationlaravel-5

Method App\Http\Requests\EndTripRequest:: rules() does not exist in file vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php on line 142


I'm new on Laravel. While I'm working with a code, I found an error when I try to call an API from the application on Android.

I'm really lost with it I don't even know what is the problem.

I don't know where to start.

ReflectionException: Method App\Http\Requests\EndTripRequest::rules() does not exist in file /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php on line 142
Stack trace:
    1. ReflectionException->() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:142
    2. ReflectionMethod->__construct() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:142
    3. Illuminate\Container\BoundMethod->getCallReflector() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:120
    4. Illuminate\Container\BoundMethod->getMethodDependencies() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:32
    5. Illuminate\Container\BoundMethod->Illuminate\Container\{closure}() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:90
    6. Illuminate\Container\BoundMethod->callBoundMethod() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:34
    7. Illuminate\Container\BoundMethod->call() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Container/Container.php:576
    8. Illuminate\Container\Container->call() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php:105
    9. Illuminate\Foundation\Http\FormRequest->createDefaultValidator() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php:84
    10. Illuminate\Foundation\Http\FormRequest->getValidatorInstance() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Validation/ValidatesWhenResolvedTrait.php:23
    11. Illuminate\Foundation\Http\FormRequest->validateResolved() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Foundation/Providers/FormRequestServiceProvider.php:30
    12. Illuminate\Foundation\Providers\FormRequestServiceProvider->Illuminate\Foundation\Providers\{closure}() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Container/Container.php:1082
    13. Illuminate\Container\Container->fireCallbackArray() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Container/Container.php:1046
    14. Illuminate\Container\Container->fireAfterResolvingCallbacks() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Container/Container.php:1031
    15. Illuminate\Container\Container->fireResolvingCallbacks() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Container/Container.php:687
    16. Illuminate\Container\Container->resolve() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Container/Container.php:615
    17. Illuminate\Container\Container->make() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:757
    18. Illuminate\Foundation\Application->make() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Routing/RouteDependencyResolverTrait.php:79
    19. Illuminate\Routing\ControllerDispatcher->transformDependency() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Routing/RouteDependencyResolverTrait.php:46
    20. Illuminate\Routing\ControllerDispatcher->resolveMethodDependencies() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Routing/RouteDependencyResolverTrait.php:27
    21. Illuminate\Routing\ControllerDispatcher->resolveClassMethodDependencies() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:41
    22. Illuminate\Routing\ControllerDispatcher->dispatch() /home/********/domains/hilove.tech/vendor/laravel/framework/src/Illuminate/Routing/Route.php:2
        responseFromServerError
D/errror: null
D/error: [Ljava.lang.StackTraceElement;@33928b1
    null

If this would help:

    public function End(EndTripRequest $request)
    {
        $merchant_id = $request->user('api-driver')->merchant_id;
        $validator = Validator::make($request->all(), [
            'booking_id' => [
                'required',
                'integer',
                Rule::exists('bookings', 'id')->where(function ($query) {
                    $query->where('booking_status', 1004);
                }),
            ],
            'latitude' => 'required',
            'longitude' => 'required',
            'tip_amount' => 'nullable|numeric',
        ]);
        if ($validator->fails()) {
            $errors = $validator->messages()->all();
            return response()->json(['result' => "0", 'message' => $errors[0], 'data' => []]);
        }
        /**
         * @var $configuration BookingConfiguration
         */
        $configuration = BookingConfiguration::where([['merchant_id', '=', $merchant_id]])->first();
        $booking_id = $request->booking_id;
        /**
         * @var $booking Booking
         */
        $booking = Booking::with('PriceCard')->find($booking_id);
        /**
         * @var $bookingDetails BookingDetail
         */
        $bookingDetails = BookingDetail::where([['booking_id', '=', $booking_id]])->first();
        $service_type_id = $booking->service_type_id;
        if (!in_array($service_type_id, array(1, 5))) {
            $start_meter_value = $bookingDetails->start_meter_value;
            $customMessages = [
                'gt' => trans_choice('api.endmeter', 3, ['value' => $start_meter_value]),
            ];
            $validator = Validator::make($request->all(), [
                'send_meter_image' => 'required',
                'send_meter_value' => 'required|integer|gt:' . $start_meter_value,
            ], $customMessages);
            if ($validator->fails()) {
                $errors = $validator->messages()->all();
                return response()->json(['result' => "0", 'message' => $errors[0], 'data' => []]);
            }
        }
        $request->user('api-driver')->free_busy = 2;
        $request->user('api-driver')->total_trips = $request->user('api-driver')->total_trips + 1;
        $request->user('api-driver')->save();
        /**
         * @var $user \App\User
         */
        $user = User::find($booking->user_id);
        $user->total_trips = $user->total_trips + 1;
        $user->save();
        if ($request->hasFile('send_meter_image')) {
            $bookingDetails->end_meter_value = $request->send_meter_value;
            $request->file('send_meter_image');
            $send_meter_image = $request->send_meter_image->store('service', 'public');
            $bookingDetails->end_meter_image = $send_meter_image;
        }
        $pricing_type = $booking->PriceCard->pricing_type;
        $price_card_id = $booking->price_card_id;
        $key = $configuration->google_key;
        $endAddress = GoogleController::GoogleLocation($request->latitude, $request->longitude, $key);
        $endAddress = $endAddress ? $endAddress : 'Address Not found';
        $endTimeStamp = strtotime('now');
        $bookingDetails->end_timestamp = $endTimeStamp;
        $bookingDetails->end_latitude = $request->latitude;
        $bookingDetails->end_longitude = $request->longitude;
        $bookingDetails->end_location = $endAddress;
        $bookingDetails->accuracy_at_end = $request->accuracy;
        $bookingDetails->save();
        $start_timestamp = $bookingDetails->start_timestamp;
        $seconds = $endTimeStamp - $start_timestamp;
        $hours = floor($seconds / 3600);
        $mins = floor($seconds / 60 % 60);
        $secs = floor($seconds % 60);
        $timeFormat = sprintf('%02d H %02d M', $hours, $mins, $secs);
        $rideTime = round(abs($endTimeStamp - $start_timestamp) / 60, 2);
        $from = $bookingDetails->start_latitude . "," . $bookingDetails->start_longitude;
        $to = $request->latitude . "," . $request->longitude;
        $coordinates = "";
        $bookingData = new BookingDataController();
        $bookingData->ActivateRefer($booking->id);
        switch ($service_type_id) {
            case "1":
                $bookingcoordinates = BookingCoordinate::where([['booking_id', '=', $request->booking_id]])->first();
                $pick = $booking->pickup_latitude . "," . $booking->pickup_longitude;
                $drop = $booking->drop_latitude . "," . $booking->drop_longitude;
                $distanceCalculation = new DistanceCalculation();
                $distance = $distanceCalculation->distance($from, $to, $pick, $drop, $bookingcoordinates['coordinates'], $merchant_id, $key);
                $distance = round($distance);
                $coordinates = $bookingcoordinates['coordinates'];
                break;
            case "5":
                $distance = GoogleController::GoogleShortestPathDistance($from, $to, $key);
                $distance = round($distance);
                break;
            default:
                $distance = $bookingDetails->end_meter_value - $bookingDetails->start_meter_value;
                $distance = $distance * 1000;
        }
        switch ($pricing_type) {
            case "1":
            case "2":
                $newArray = PriceController::CalculateBill($price_card_id, $distance, $rideTime, $booking_id, $bookingDetails->wait_time, $bookingDetails->dead_milage_distance, $booking->User->outstanding_amount);
                if (!empty($configuration->toll_api)) {
                    $newTool = new Toll();
                    $toolPrice = $newTool->checkToll($configuration->toll_api, $from, $to, $coordinates, $configuration->toll_key);
                    if (is_array($toolPrice) && array_key_exists('cost', $toolPrice)) {
                        if ($toolPrice['cost'] > 0) {
                            $parameter[] = array('price_card_id' => $price_card_id, 'booking_id' => $booking_id, 'parameter' => "TollCharges", 'amount' => sprintf("%0.2f", $toolPrice['cost']), 'type' => "CREDIT", 'code' => "");
                            array_push($newArray, $parameter);
                        }
                    }
                }
                $newExtraCharge = new ExtraCharges();
                $carditnewArray = array_filter($newArray, function ($e) {
                    return ($e['type'] == "CREDIT");
                });
                $amount = array_sum(Arr::pluck($carditnewArray, 'amount'));
                if ($booking->number_of_rider > 1) {
                    $amount = $amount + $booking->PriceCard->extra_sheet_charge;
                }
                $booking_time = $booking->booking_type == 1 ? $booking->created_at->toTimeString() : $booking->later_booking_time;
                $timeCharge = $newExtraCharge->nightchargeEstimate($price_card_id, $booking_id, $amount, $booking_time);
                if (!empty($timeCharge)) {
                    $charge = array_sum(Arr::pluck($timeCharge, 'amount'));
                    $amount = $amount + $charge;
                    $newArray = array_merge($newArray, $timeCharge);
                }
                if ($booking->PriceCard->sub_charge_status == 1) {
                    $surge = $booking->PriceCard->sub_charge_type == 1 ? $booking->PriceCard->sub_charge_value : bcdiv($amount, $booking->PriceCard->sub_charge_value, 2);
                    $amount = $amount + $surge;
                    $parameter = array('price_card_id' => $price_card_id, 'booking_id' => $booking_id, 'parameter' => "Surge-Charge", 'amount' => sprintf("%0.2f", $surge), 'type' => "CREDIT", 'code' => "");
                    array_push($newArray, $parameter);
                }
                $discoutArray = array_filter($newArray, function ($e) {
                    return ($e['type'] == "DEBIT");
                });
                if (!empty($discoutArray)) {
                    $promoDiscount = sprintf("%0.2f", array_sum(Arr::pluck($discoutArray, 'amount')));
                    $bookingDetails->promo_discount = $promoDiscount;
                    $amount = $amount > $promoDiscount ? $amount - $promoDiscount : '0.00';
                } else {
                    $bookingDetails->promo_discount = "0.00";
                }
                $referDiscount = $bookingData->Refer($booking->user_id);
                if (!empty($referDiscount)) {
                    switch ($referDiscount->offer_type) {
                        case "1":
                            $referAmount = $amount;
                            $amount = 0;
                            break;
                        case "2":
                            $referAmount = ($amount * $referDiscount->referral_offer_value) / 100;
                            $amount = $amount - $referAmount;
                            break;
                        case "3":
                            $referAmount = $referDiscount->referral_offer_value;
                            $amount = $amount < $referAmount ? 0 : $amount - $referAmount;
                            break;
                    }
                    $parameter[] = array('price_card_id' => $price_card_id, 'booking_id' => $booking_id, 'parameter' => "Promotion", 'amount' => sprintf("%0.2f", $referAmount), 'type' => "DEBIT", 'code' => "");
                    array_push($newArray, $parameter);
                }
                $billDetails = json_encode($newArray);
                $bookingDetails->total_amount = sprintf("%0.2f", $amount);
                $payment = new Payment();
                if ($amount > 0) {
                    $payment->MakePayment($booking->id, $booking->payment_method_id, $amount, $booking->user_id, $booking->card_id);
                } else {
                    $payment->UpdateStatus($booking->id);
                }
                $bookingDetails->bill_details = $billDetails;
                $bookingDetails->save();
                \App\Http\Controllers\Helper\CommonController::Commission($booking_id, $amount);
                if ($booking->User->outstanding_amount) {
                    User::where([['id', '=', $booking->user_id]])->update(['outstanding_amount' => NULL]);
                }
                break;
            case "3":
                $amount = "";
                break;
        }
        if ($service_type_id == 5) {
            $poolRide = new PoolController();
            $poolRide->DropPool($booking, $request);
        }
        $distance = round($distance / 1000, 2) . " Km";
        $booking->booking_status = 1005;
        $booking->travel_distance = $distance;
        $booking->travel_time = $timeFormat;
        $booking->travel_time_min = $rideTime;
        $booking->final_amount_paid = sprintf("%0.2f", $amount);
        $booking->save();
        $user_id = $booking->user_id;
        $message = "Driver End Ride";
        $userdevices = UserDevice::where([['user_id', '=', $user_id]])->get();
        $playerids = Arr::pluck($userdevices, 'player_id');
        $data = $bookingData->BookingNotification($booking);
        Onesignal::UserPushMessage($playerids, $data, $message, 1, $booking->merchant_id);
        return response()->json(['result' => "1", 'message' => trans('api.message15'), 'data' => $booking]);
    }

And this is the content of EndTripRequest

    <?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\UploadedFile;

/**
 * Class EndTripRequest
 *
 * @property int booking_id


 * @property double send_meter_value


 * @property UploadedFile|UploadedFile[]|array|null send_meter_image


 * @property double latitude


 * @property double longitude


 * @property int accuracy


 *
 * @package App\Http\Requests


 */


class EndTripRequest extends FormRequest
{


    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */


    public function authorize()
    {
        return true;
    }

}

Solution

  • Like the error suggests, you don't have rules method in you Request class. I recommend you to read this from laravel docs.

    In your case, you should add following code below authorize() methods

    public function rules()
    {
        return [
            'booking_id'                 => ['required', 'integer', 'exists:bookings,id'],
            'send_meter_value'                  => ['numeric'],
            'uploaded_files[]'                  => ['nullable', 'array'],
            'uploaded_files.*'   => 'file'
            //.... And validation for your other fields
        ];
    }
    

    To learn about available validation rules in laravel, look into this