laravel – uploading files

Hello everyone today I am trying to build a Laravel application that is an e-commerce website. I am just learning about file uploads using this framework that end up resulting in this error when I try uploading something:

> MethodNotAllowedHttpException in RouteCollection.php line 219:

    in RouteCollection.php line 219
    at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE')) in RouteCollection.php line 206
    at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE')) in RouteCollection.php line 158
    at RouteCollection->match(object(Request)) in Router.php line 823
    at Router->findRoute(object(Request)) in Router.php line 691
    at Router->dispatchToRoute(object(Request)) in Router.php line 675
    at Router->dispatch(object(Request)) in Kernel.php line 246
    at Kernel->IlluminateFoundationHttp{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
    at Pipeline->IlluminateRouting{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
    at CheckForMaintenanceMode->handle(object(Request), object(Closure))
    at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
    at Pipeline->IlluminatePipeline{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
    at Pipeline->IlluminateRouting{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
    at Pipeline->then(object(Closure)) in Kernel.php line 132
    at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
    at Kernel->handle(object(Request)) in index.php line 54

I’ve looked at the laracast videos on that website and found an outdated video on file uploads. I am somewhat new to the Laravel framework and I am hoping I get some help with uploading and validating files

Here’s what my routes.php file looks like

        /*
    |--------------------------------------------------------------------------
    | Application Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register all of the routes for an application.
    | It's a breeze. Simply tell Laravel the URIs it should respond to
    | and give it the controller to call when that URI is requested.
    |
    */
    Route::resource('item', 'ItemController');

    Route::get('welcome', function() {
        return view('welcome');
    });

    Route::post('item', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b625f4e466844455f594447474e596b785f44594e">[email protected]</a>');
    Route::auth();

    Route::get('/home', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1d557270785e7273696f727171786f5d7473797865">[email protected]</a>');

EDIT my item controller

    <?php

namespace AppHttpControllers;

use Request;

use AppHttpRequests;

use AppItem;
class ItemController extends Controller
{
    public function index()
    {
        //fetch all items from the database
        $items = Item::All();
        return $items;
    }

    public function show($id)
    {

        $item = Item::find($id);

        if (is_null($item)) {
            abort(404);
        }
        return view('item.show', compact('item'));
    }

    /**
     * @return mixed
     */
    public function create()
    {
        return view('item.create');
    }

    public function store(RequestsCreateItem $request)
        {


            Item::create($request->all());

            if($request->hasFile('filename')) {
                $file = $request->file('filename');
                if($request->file('photo')->isValid()) {
                    $request->file('filename')->move('/uploads');
                }

            }

        }

My Form code
    @extends('app');
@section('content');
    <h1>Add a  new item</h1>
    <hr />
    <content>
        <div class="form-group">
        {!! Form::open() !!}
        {!! Form::label('name', "Name") !!}
        {!! Form::text('name', null, ['class' => 'form-control']) !!}

       {!! Form::label('filename', "File Name") !!}
        {!! Form::file('filename', null, ['class' => 'form-control']) !!}

        {!! Form::label('description', 'Description') !!}
        {!! Form::textarea('description', null, ['class' => 'form-control']) !!}
        {!! Form::submit('Add Item', ['class' => 'btn btn-primary form-control']) !!}

    </content>
</div>

@stop

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

//Route
Route::post('item', ['as' => 'item.store', 'uses' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="88c1fcede5cbe7e6fcfae7e4e4edfac8fbfce7faed">[email protected]</a>']);

//Form

replace


{!! Form::open() !!}

with


{!! Form::open(['route' => 'item.store', 'files' => true]) !!}`


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x