vendredi 22 mars 2019

Laravel 5.8 PDF upload

for my website I need a form which includes a file upload for PDF files, but I'm new to these and don't really know how to do it.

This is what I got so far, but keep getting: "Too few arguments to function App\Http\Controllers\FileController::create(), 0 passed and exactly 1 expected"

Controller:

<?php

namespace App\Http\Controllers;

use App\User;
use App\Payment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class FileController extends Controller
{
    public function index(){

    $users = User::all();
    return view('fileupload.create', compact('users'));
}

protected function create(array $data)
{
    $request = app('request');
    if($request->hasfile('file')){
        $file = $request->file('file');
        $filename = $file['filename']->getClientOriginalExtension();
        Storage::make($file)->save( public_path('/storage/loonstrookjes/' . $filename) );
        dd($filename);

    }


    return Payment::create([
        'file_name' => $filename,
        'file_path' => '/storage/loonstrookjes/',
        'user_id' => $data['employee'],
    ]);

    return route('fileupload.create');
}

}

Model User:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Kyslik\ColumnSortable\Sortable;

class User extends Authenticatable
{
    use Notifiable;
    use Sortable;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $table = 'users';

    protected $fillable = [
        'username', 'first_name', 'last_name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

Model Payment:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Payment extends Model
{
    protected $table = 'payment_list';

    protected $fillable = [
        'user_id', 'file_name', 'file_path'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'id', 'file_name', 'file_path'
    ];
}

View:

@extends('layouts.master')

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

                    <div class="card-body">
                        <form method="POST" action="" enctype="multipart/form-data">
                            @csrf

                            <div class="form-group row">
                                <label for="filename" class="col-md-4 col-form-label text-md-right"></label>

                                <div class="col-md-6">
                                    <input id="filename" type="text" class="form-control" name="filename" value="" required autofocus>

                                    @if ($errors->has('filename'))
                                        <span class="invalid-feedback" role="alert">
                                        <strong></strong>
                                    </span>
                                    @endif
                                </div>
                            </div>

                            <div class="form-group row">
                                <label for="file" class="col-md-4 col-form-label text-md-right"></label>

                                <div class="col-md-6">
                                    <input id="file" type="file" class="form-control" name="file">
                                </div>
                            </div>

                            <div class="form-group row">
                                <label for="usertype" class="col-md-4 col-form-label text-md-right"></label>

                                <div class="col-md-6">
                                    <select class="form-control" name="type">
                                        @foreach($users as $user)
                                        <option value=""> </option>
                                            @endforeach
                                    </select>
                                </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

I hope someone can help me find out what's wrong or a better way to do this. Thank you in advance!!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire