Just Another Guy
Just Another Guy

Reputation: 127

Laravel - controller doesn't see image input

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?

Upvotes: 1

Views: 300

Answers (2)

User787665
User787665

Reputation: 337

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

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

Upvotes: 0

MrEduar
MrEduar

Reputation: 1931

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

Upvotes: 1

Related Questions