1 year ago

#304119

test-img

Bilal Ahmed

Validator not validating the request in Laravel 8

I am inserting the data. The data is being entering quite fine but whenever I enter a letter the entry is done but that entry is converted to '0'. This is my controller store function:

public function store(GuidanceReportRequest $request)
{
    $stats = GuidanceReport::where('user_id', $request->user_id)->whereDate('created_at', now())->count();
    if ($stats > 0) {
        Session::flash('warning', 'Record already exists for current date');
        return redirect()->route('reports.index');
    }
    
    if ((!empty($request->call_per_day[0]) && !empty($request->transfer_per_day[0])) ||
        (!empty($request->call_per_day[1]) && !empty($request->transfer_per_day[1])) || (!empty($request->call_per_day[2])
            && !empty($request->transfer_per_day[2]))
    ) {
        foreach ($request->category as $key => $value) {
            $catgeory_id = $request->category[$key];
            $call_per_day = $request->call_per_day[$key];
            $transfer_per_day = $request->transfer_per_day[$key];
            if (!empty($catgeory_id) && !empty($call_per_day) && !empty($transfer_per_day)) {
                GuidanceReport::create([
                    "user_id" => $request->user_id,
                    "categories_id" => $catgeory_id,
                    "call_per_day" => $call_per_day,
                    "transfer_per_day" => $transfer_per_day,
                ]);
            }
        }
    } else {
        GuidanceReport::create($request->except('category', 'call_per_day', 'transfer_per_day'));
    }
    Session::flash('success', 'Data Added successfully!');
    return redirect()->route('reports.index');
}

This is my Validation Request code

public function rules()
{
    $rules = [];
    
    $request = $this->request;
    if ($request->has('transfer_per_day')) {
        if (!empty($request->transfer_per_day)) {
            $rules['transfer_per_day'] = "numeric";
        }
    }
    if ($request->has('call_per_day')) {
        if (!empty($request->call_per_day)) {
            $rules['call_per_day'] = "numeric";
        }
    }
    if ($request->has('rea_sign_up')) {
        if (!empty($request->rea_sign_up)) {
            $rules['rea_sign_up'] = "numeric";
        }
    }
    if ($request->has('tbd_assigned')) {
        if (!empty($request->tbd_assigned)) {
            $rules['tbd_assigned'] = "numeric";
        }
    }
    if ($request->has('no_of_matches')) {
        if (!empty($request->no_of_matches)) {
            $rules['no_of_matches'] = "numeric";
        }
    }
    if ($request->has('leads')) {
        if (!empty($request->leads)) {
            $rules['leads'] = "numeric";
        }
    }
    if ($request->has('conversations')) {
        if (!empty($request->conversations)) {
            $rules['conversations'] = "numeric";
        }
    }
    return $rules;
}

Although I check the type in which request is being sent from controller and recieved from the request validation and it is Object. So how can I solve the issue.

laravel-8

laravel-validation

0 Answers

Your Answer

Accepted video resources