Home > other >  Laravel errors while storing a file
Laravel errors while storing a file

Time:09-28

I am working on a laravel crud project. Now i want to store files like .xlsx and .docx But i keep getting errors in my controller and browser:

Controller:

   public function store(Request $request)
    {
        $request->validate([
            'title'=>'required',
            'description_short'=>'',
            'description_long'=>'',
            'file'=>'',
            'language_id'=> [
                'required', 'exists:language,id'
            ],
        ]);
        $fileName = $request->file->getClientOriginalName();
        $filePath = 'files/' . $fileName;
        $path = Storage::disk('public')->put($filePath, file_get_contents($request->file));
        $path = Storage::disk('public')->url($path);

        $file = new File([
            'title'=> $request->get('title'),
            'description_short'=> $request->get('description_short'),
            'description_long'=> $request->get('description_long'),
            'file'=>$request->get('file'),
            'language_id'=> $request->language_id,
        ]);
        $file->save();
       
        return back();
    }

Here i get the error: Undefined method 'url'

Create page:

     <form method="post" action="{{ route('admin.language.store') }}" enctype="multipart/form-data">
          @csrf
          <div >    
              <label for="title">{{('name')}}</label>
              <input type="text"  name="name"/>
          </div>
          <div >
              <label for="value">{{('file')}}</label>
              <input type="file"  name="file"/>
          </div>        
     
          <button type="submit" >Add language</button>
      </form>

the browser error i get is : Call to a member function getClientOriginalName() on string.

if i need to provide more information i will gladly do so!

CodePudding user response:

issue is with your request can you please dd your request and show me

CodePudding user response:

file is reserved keyword in Request class to get submitted Files in post method.

You can not use file in input. So first you have to change file name in input box.

After that you can do like below.

$request->file('file_input_name')->getClientOriginalName();

  • Related