1

My form:

<form action="{{route('settings.update')}}" method="POST">
    @csrf
    <div class="col-sm-3">
        <div class="form-group">
            <label for="avatar">Upload a new one:</label>
            <input type="file" id="pic" name="pic"/>
         </div>
     </div>
     <div class="form-group">
         <button type="submit" class="btn btn-primary">Upload avatar</button>
     </div>
 </form>

My Controller:

public function update_settings($request)
{
    $this->validate($request, [
        'pic' => 'required|image'
    ]);
    $path = $request->image->store('/img/');
    Auth::user()->image = $path;
    return view('pages.settings');
}

The error:

Too few arguments to function App\Http\Controllers\PagesController::update_settings(), 0 passed and exactly 1 expected

I am passing only the image file in the $request, but for some reason the controller doesn't see it, what am I doing incorrectly?

2 Answers 2

1

To obtain an instance of the current HTTP request via dependency injection, you should type-hint the Illuminate\Http\Request class on your controller constructor or method. The current request instance will automatically be injected by the service container:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class UserController extends Controller
{
    /**
     * Store a new user.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        $name = $request->input('name');

        //
    }
}

Look well that in the method I am injecting the dependence of Illuminate\Http\Request

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

2 Comments

I do have that included. use App\Product; use App\ProductSpecifics; use Illuminate\Http\Request;
You may have it included but if you don't put it in the method it won't work. Look at the method in my example and you will see that I put store(Request $request) and you only have update_settings($request)
0

You need to add enctype='multipart/form-data' to your opening form tag

<form action="{{route('settings.update')}}" method="POST" enctype="multipart/form-data">

Comments

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.