Farirai's avatar

Method Illuminate\Validation\Validator::validateCsv,txt,xlx,xls,pdf does not exist.

below is my code want to upload a file to app/public/file directory in laravel orchid but getting that error

i am using laravel orchid

\<?php

namespace App\Orchid\Screens\Examples;

use Illuminate\Http\Request;
use App\Models\Upload;
use App\Models\Contact;
use Maatwebsite\Excel\Concerns\ToModel;
use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Mail;
use Orchid\Screen\Fields\Input;
use Orchid\Screen\Fields\Quill;
use Orchid\Screen\Fields\Relation;
use Orchid\Screen\Fields\Textarea;
use Orchid\Support\Facades\Layout;
use Orchid\Screen\Actions\Button;
use Orchid\Screen\Screen;
use Orchid\Attachment\Attachable;
use Orchid\Attachment\Models\Attachment;
use App\Models\File;
use Orchid\Support\Facades\Alert;


class ExampleFieldsAdvancedScreen extends Screen
{
    /**
     *
     * @return array
     */
    public function query(): iterable
    {
        return [
        ];
    }

    /**
     * Display header name.
     *
     * @return string|null
     */
    public function name(): ?string
    {
        return 'Bulk Add or Bulk Send';

    }


    /**
     * Display header description.
     *
     * @return string|null
     */
    public function description(): ?string
    {
        return 'Upload contact csv or create new.';
        
        
    }


    /**
     * Button commands.
     *
     * @return Action[]
     */
    public function commandBar(): iterable
    {
        return [
            Button::make('Upload')
            ->method('store')
            ->icon('paper-plane')
           



            
                    
        ];
    }

      /**
       * @param Request $request
     */
    //upload the csv file
   
    /**
     * Views.
     *
     * @throws \Throwable
     *
     * @return \Orchid\Screen\Layout[]
     */
    public function layout(): iterable
    {
        return [

        Layout::rows([

                Input::make('file')
                    ->type('file')
                    ->title('Upload file')
                    ->horizontal(),

                // Relation::make('contacts.')
                //     ->title('recipients')
                //     ->multiple()
                //     ->required()
                //     ->placeholder('Mobile Number')
                //     ->help('Enter the users that you would like to send this message to.')
                //     ->fromModel(Contact::class,'name','mobile'),

               

                // TextArea::make('post.description')
                //     ->title('Enter the message you want to send')
                //     ->rows(3)
                //     ->maxlength(200)
                //     ->placeholder('Currently Flex is Down'),
                
                // Button::make('Send Message')
                //     ->method('sendMessage')
                //     ->icon('paper-plane'),

                Button::make(' Upload file')
                    ->method('store')
                    ->icon('link')
                    ->icon('folder')
                    ->horizontal(),


              
                    

 ])->title('Contacts upload'),

        
        ];

        
    }

    public function store(Request $request)
    {
         
        $validatedData = $request->validate([
         'file' => 'required|csv,txt,xlx,xls,pdf|max:4048',
 
        ]);
 
        $name = $request->file('file')->getClientOriginalName();
 
        $path = $request->file('file')->store('public/files');
 
 
        $save = new File;
 
        $save->name = $name;
        $save->path = $path;
 
        return Alert::info('You have successfully uploaded a file.');
 
    }
}



0 likes
13 replies
Sergiu17's avatar

@Farirai different error, making progress!

I don't know what's on line 146, check the syntax maybe

1 like
kokoshneta's avatar

@Farirai Line 146 is the line @sergiu17 changed, and the => is correct there. Are you sure you copied it back in correctly? No stray quote marks anywhere?

Farirai's avatar

what i did use Illuminate\Support\Facades\Validator;

public function store(Request $request)
{
     



$validator = Validator::make($request->all(), [
    'file' =>'required|mimes:csv,txt,xlx,xls,pdf|max:4048',
]);

   

    $name = $request->file('file')->getClientOriginalName();

    $path = $request->file('file')->store('public/files');


    $save = new File;

    $save->name = $name;
    $save->path = $path;

    return ('File Has been uploaded successfully in laravel');

}
1 like
kokoshneta's avatar

@Farirai That’s not a solution. You’re not actually validating anything, just creating a validator instance (which should not be necessary to begin with). The code will run even if the files don’t pass the validation.

Also, why do you bother creating the $save object when you don’t use it for anything?

This should be enough for what you actually do in the code you posted here:

public function store(Request $request) {	
	$request->validate(['file' => 'required|mimes:csv,txt,xlx,xls,pdf|max:4048']);
	$path = $request->file('file')->store('public/files');
	return Alert::info('You have successfully uploaded a file.');
}
Farirai's avatar

@kokoshneta i am getting the error below after doing what you said

Method Illuminate\Validation\Validator::validateCsv,txt,xlx,xls,pdf does not exist.

Farirai's avatar
Farirai
OP
Best Answer
Level 3

this worked public function store(Request $request) {

    $request->validate([
        'file' => 'required|mimes:png,jpg,jpeg,csv,txt,xlx,xls,pdf|max:2048'
        ]);
        
    $name = $request->file('file')->getClientOriginalName();

    $path = $request->file('file')->store('public/files');
    
    return back()
            ->with('success','File has uploaded to the database.')
            ->with('file', $name);
}

Please or to participate in this conversation.