lundi 1 octobre 2018

File will not upload using laravel 5.7 forms

I switched from the laravel collective forms of 5.4 to the forms of 5.7. everything was working perfectly until I switched and tried using the new forms. I'm trying to upload a simple picture with a text post. The text is uploaded in the database, but the picture/file is not. I'm not sure what I'm missing so if anyone can assist it would greatly appreciated. Thanks in advance.

home.blade.php:

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Dashboard</div>

                <div class="card-body">
                    @if (session('status'))
                        <div class="alert alert-success" role="alert">
                            
                        </div>
                    @endif

                        <form method="POST" enctype="multipart/form-data" action="">
                            @csrf

                            <div class="form-group row">
                                <label for="body" class="col-md-4 col-form-label text-md-right"></label>
                                <div class="col-md-6">
                                    <input id="body" type="text" class="form-control" name="body" value="">
                                </div>
                            </div>

                            <div class="form-group row">
                                <label for="photo" class="col-md-4 col-form-label text-md-right"></label>
                                <div class="col-md-6">
                                    <input id="photo" type="file" class="form-control" name="photo" value="">
                                </div>
                            </div>

                            <div class="form-group row mb-0">
                                <div class="col-md-6 offset-md-4">
                                    <button type="submit" class="btn btn-primary">
                                        
                                    </button>
                                </div>
                            </div>
                        </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection 

PostsController.php:

public function store(Request $request)
{
    $input = $request->all();

    $user = Auth::user();

    if($file = $request->file('photo_id')) {
        $name = time() . $file->getClientOriginalName();
        $file->move('images', $name);
        $photo = Photo::create(['file'=>$name]);
        $input['photo_id'] = $photo->id;
    }

    $user->post()->create($input);
    return redirect('/home');
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire