malhayek
770
14
Laravel

Form-request class does not exist!

Posted 8 months ago by malhayek

I am trying to use a form-request class to validate an incoming update request. But every time I post data to the process method, I get the

Reflection Error: Class App\Http\Requests\ReservationPickupsFormRequest does not exist

I am not sure what could be causing this issue.

I have executed composer dumpautoload command and it did not help.

I created the following routes

Route::group([
    'prefix' => 'reservation_pickups',
], function () {
    Route::get('/', '[email protected]')
        ->name('reservation_pickups.reservation_pickup.index');
    Route::get('/pickup/{id}', '[email protected]')
        ->name('reservation_pickups.reservation_pickup.pickup')
        ->where('id', '[0-9]+');
    Route::post('/process/{id}', '[email protected]')
        ->name('reservation_pickups.reservation_pickup.process');
});

My form-request class looks like this

<?php

namespace App\Http\Requests;

use Auth;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ReservationPickupsFormRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'current_miles' => 'nullable|numeric|min:100',
            'payments' => 'required|array'
            'payments.*.method' => [
                'required',
                Rule::in(['cash', 'bank_card', 'check', 'credit']),
            ],
            'payments.*.amount' => 'required|numeric|min:0',
            'payments.*.due_date' => [
                'date_format:j/n/Y',
                'required_if:payments.*.method,check',
                'required_if:payments.*.method,credit',
            ],
        ];

        return $rules;
    }

    /**
     * Get the request's data from the request.
     *
     *
     * @return array
     */
    public function getData()
    {
        $data = $this->only(['current_miles', 'payments']);

        return $data;
    }
}

My controller looks like this

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Requests\ReservationPickupsFormRequest;
use App\Models\Reservation;
use Carbon\Carbon;

class ReservationPickupsController extends Controller
{
    /**
     * Display a listing of the reservations.
     *
     * @return Illuminate\View\View
     */
    public function index()
    {
        $reservations = $this->getPickupQuery()->paginate(25);

        return view('reservation_pickups.index', compact('reservations'));
    }

    /**
     * Show the form for creating a new reservation.
     *
     * @return Illuminate\View\View
     */
    public function pickup($id)
    {
        $reservation = $this->getPickupQuery()->findOrFail($id);

        return view('reservation_pickups.pickup', compact('reservation'));
    }

    /**
     * Process the pickup of the reservation.
     *
     * @param int $id
     * @param App\Http\Requests\ReservationPickupsFormRequest $request
     *
     * @return Illuminate\View\View
     */
    public function process(ReservationPickupsFormRequest $request)
    {
        dd('request', $request->getData());

        $reservation = $this->getPickupQuery()->findOrFail($id);

        // process the update
    }

    /**
     * Get the base query builder for the pickable reservations
     *
     * @return Illuminate\Database\Eloquent\Builder builder
     **/
    private function getPickupQuery()
    {
        $query = Reservation::with('primaryDriver', 'vehicle', 'additionalDrivers')
            ->where('status', 'scheduled')
            ->where('reserved_from', '<=', Carbon::now()->endOfDay());

        return $query;
    }
}

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

Reply to

Use Markdown with GitHub-flavored code blocks.