skip to Main Content

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 class="form-group">    
              <label for="title">{{('name')}}</label>
              <input type="text" class="form-control" name="name"/>
          </div>
          <div class="form-group">
              <label for="value">{{('file')}}</label>
              <input type="file" class="form-control" name="file"/>
          </div>        
     
          <button type="submit" class="btn btn-primary">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!

2

Answers


  1. Chosen as BEST ANSWER
    $file = $request->file->getClientOriginalName();
    

    fixed it


  2. 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();

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search