0

This function is the function I use to store a new company:

public function store(Request $request)
{   

    $file = $request->file('logo');
    $filename = 'company-logo-' . time() . '.' . $file->getClientOriginalExtension();
    $path = $file->storeAs('public', $filename);
    dd($path);            

    Company::create([
        'name' => $request->name,
        'email' => $request->email,
        'logo' => $request->logo,
        'website' => $request->website
    ]);

    return redirect('/company/all');
}

This view is the one with the form:

@extends('layouts.app')

@section('content')
    <div class="card">
        <div class="card-body">
            <h4 class="card-title">Create a Company</h4>
        </div>

        <div class="container">
            <div class="jumbotron">
                <ul class="list-group">
                    <li class="list-group-item">
                        <h3>Enter Company Information:</h3>
                        <form action="{{ route('company.store') }}" enctype="mutlipart/form-data" method="POST">
                            @csrf
                            <div class="form-group">
                                <input type="text" class="form-control" name="name" placeholder="Company name" value="{{ old('name') }}">
                            </div>
                            <div class="form-group">
                                <input type="text" class="form-control" name="email" placeholder="Email" value="{{ old('email') }}">
                            </div>
                            <div class="form-group">
                                <input class="form-control" type="file" name="logo">
                            </div>
                            <div class="form-group">
                                <input class="form-control" type="url" name="website" placeholder="Website" value="{{ old('website') }}">
                            </div>
                            <button class="btn btn-primary" type="submit">Add Company</button>
                        </form>
                    </li>
                </ul>
            </div>
        </div>
    </div>
@endsection

This is the route:

Route::post('/add', 'CompaniesController@store')->name('store');

Well, what happens when I try to submit this form is that $file variable always returned with null:

Error
Call to a member function getClientOriginalExtension() on null
http://localhost:8000/company/add

Basically what I want to do is send the name of the image to the database and upload the image to my public folder. Neither happens with this code. when I erase the part starting from $file till dd($path); it adds the values to the database but the image hasn't been uploaded.

Any help? Thanks in advance.

Ali
  • 15
  • 1
  • 8

5 Answers5

1

First of all the attribute on the form is wrong which is enctype="mutlipart/form-data" and it should be enctype="multipart/form-data"

or an alternative you can use below code according to your requirements:

if($request->hasFile('logo')){
        $file = $request->file('logo');
        $fileName = 'company-logo-' .time().$file->getClientOriginalName();
        Storage::put('public/'.$fileName,file_get_contents($file));
        now you can store the $filename variable in database and image will be uploaded to storage/app/public folder
    }

please add use Storage on top of file and run php artisan storage:link to make symbolic link between storage folder and public folder

Naveed Ali
  • 1,043
  • 7
  • 15
0

try changing to:

public function store(Request $request)
{   

    $file = $request->file('logo');
    $path = '';
    if($file) {
        $filename = 'company-logo-' . time() . '.' . $file->getClientOriginalExtension();
        $path = $file->storeAs('public', $filename);
    }            

    Company::create([
        'name' => $request->name,
        'email' => $request->email,
        'logo' => $path,
        'website' => $request->website
    ]);

    return redirect('/company/all');
}
SviesusAlus
  • 429
  • 4
  • 12
  • Tried it before, it gives me no error but it doesn't upload the file to my public folder it just writes the file name for the database. – Ali Aug 18 '20 at 14:08
  • the file is not in `project_root/storage/app/public/` ? – SviesusAlus Aug 18 '20 at 14:13
  • That path you mentioned is connected to my public folder as I used the php artisan storage:link and the file won't upload to it. – Ali Aug 18 '20 at 14:18
0
  if (Input::hasFile('logo')) {
                $file = Input::file('logo');
                $ext = $file->getClientOriginalExtension();
                $file_name = 'company-logo-' . time() . ".{$ext}";
                $path = base_path().'/public/';
                $file->move($path , $file_name);
            }
  • use Illuminate\Support\Facades\Input; I used this to import Input but an error keeps popping up that it doesn't exist. – Ali Aug 18 '20 at 14:16
0

First you change enctype="multipart/form-data" instead of enctype="mutlipart/form-data" in your form. Then put this code to your controller

public function store(Request $request)
{
  if($request->hasFile('logo')) {
    $img_ext = $request->file('logo')->getClientOriginalExtension();
    $filename = 'company-logo-' . time() . '.' . $img_ext;
    $path = $request->file('logo')->move(public_path(), $filename);//image save public folder
  }
  //You should store only filename not path in db
  Company::create([
    'name' => $request->name,
    'email' => $request->email,
    'logo' => $filename, 
    'website' => $request->website
  ]);

    return redirect('/company/all');
}
Sobir
  • 167
  • 2
  • 7
  • That's not it. Please note that this condition in the IF statement is always false and $request->file('logo') returns null. If I can get it not to return null it would be solved. – Ali Aug 18 '20 at 14:57
  • Do you change `enctype="multipart/form-data"` instead of `enctype="mutlipart/form-data"` in your form? – Sobir Aug 18 '20 at 14:59
  • What do you mean? You wrote two identical statements. – Ali Aug 18 '20 at 15:01
  • You write `mutlipart` but it is wrong and it should be `multipart` – Sobir Aug 18 '20 at 15:03
  • Aha. Still the nothing improves – Ali Aug 18 '20 at 15:21
  • I checked your code using dd($request->file('logo')). It works and doesn't return null. – Sobir Aug 18 '20 at 15:34
  • Only this `$file->storeAs('public', $filename);`doesn't work. It return `"public/company-logo-1597765018.jpg"` . Use `$request->file('logo')->move(public_path(), $filename);` it works – Sobir Aug 18 '20 at 15:38
0

You can use the file pond. The core library is written in vanilla JavaScript and therefore can be used everywhere. Visit: https://pqina.nl/filepond/

Murad
  • 1,064
  • 16
  • 13