0

I have simple Form:

<form method="post" action="{{ route('company.store') }}">
    Logo: <br>
    <input name="logo" type="file"><br>

In my controller i try to save image with

$file = $request->file('logo')->store('avatars');

but i have error

 "Call to a member function store() on null"

dd($request->file('logo'); shows 'null'

How to reach file to save it?

1

2 Answers 2

4

To upload a file you need to add enctype="multipart/form-data" to you opening form tag:

<form method="post" action="{{ route('company.store') }}" enctype="multipart/form-data">

If you don't include it then only the file name will be submitted and will cause

$request->file('logo')

to return null because it isn't a file it's a string.

Sign up to request clarification or add additional context in comments.

1 Comment

I linked storage to public directory with php artisan storage:link, but i can't show images from there. If i use <img src="{{ asset ('storage/app/avatars/LOW6Fc2TBH8UoexcXLntQkzncXSDN6OsIt7KLbiG.jpeg') }}"> image doesn't shows. What i do wrong?
2

uploading file need a enctype="multipart/form-data"

      <form action="{{ route('company.store') }}" method="post" enctype="multipart/form-data"
             class="form-material">
               {{csrf_field()}}
          <div class="form-body">         
                 <h3 class="card-title">upload image</h3>
             <div class="form-group">
                <label class="control-label">image 1</label>
                <input type="file" name="image_path" class="form-control">
             </div>
          </div>
    </form>



Your Controller should look like this .


public function store(Request $request)
    {
        $this->validate($request,['image_path'=> 'required|image']);

        $company = new Company();
        if($request->hasFile('image_path'))
        {
      $company->image_path= $request->file('image_path')->store('company','public');
        }
        
        $company->save();

          return back()->with('success', 'Done!');

    }

3 Comments

Your code store image on public folder successfully but it returns xampp's tmp folder path in return in $company->image_path which should be the name of file which is actually stored in public folder.
when u want to use the image_path you should do this >> {{url('/storage/'.$company->image_path);}} then it will work
sorry, your example works fine i have got name of file like phph45.tmp because of file object of laravel but your example returns file name with the path, but most of we need only filename so i have used $request->file('image')->hashName() and it is working, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.