duanjurong1347 2019-03-22 10:53
浏览 53
已采纳

Laravel 5.8 PDF上传

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">{{ __('Loonstrook uploaden') }}</div>

                    <div class="card-body">
                        <form method="POST" action="{{ route('create') }}" enctype="multipart/form-data">
                            @csrf

                            <div class="form-group row">
                                <label for="filename" class="col-md-4 col-form-label text-md-right">{{ __('Bestandsnaam') }}</label>

                                <div class="col-md-6">
                                    <input id="filename" type="text" class="form-control{{ $errors->has('filename') ? ' is-invalid' : '' }}" name="filename" value="{{ old('filename') }}" required autofocus>

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

                            <div class="form-group row">
                                <label for="file" class="col-md-4 col-form-label text-md-right">{{ __('Bestand') }}</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">{{ __('Werknemer:') }}</label>

                                <div class="col-md-6">
                                    <select class="form-control" name="type">
                                        @foreach($users as $user)
                                        <option value="{{$user->id}}">{{$user->first_name}} {{$user->last_name}}</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">
                                        {{ __('Uploaden') }}
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

These are my routes:

Route::get('/create', 'FileController@index')->name('create'); 
Route::post('/create', 'FileController@create'); 

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

EDIT:

Your answers have helped me quite a bit, but now I'm facing another issue...

The controller now looks like this:

<?php

namespace App\Http\Controllers;

use App\User;
use App\Payment;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\File\UploadedFile;
UploadedFile::getMaxFilesize();

class FileController extends Controller
{
    public function index(){

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

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'filename' => ['required', 'string', 'max:255'],
            'file' => ['required', 'file'],
            'user_id' => ['required'],
        ]);
    }

    protected function create(Request $request)
    {
        $request = app('request');
        if($request->hasfile('file')){
            $file = $request->file('file');
            $filename = $request->input('filename');
            $file = $filename . '.' . $file->getClientOriginalExtension();
            $file_path = storage_path('/loonstrookjes');
            Storage::disk('local')->putFile($file_path, new File($request->file), $file);
            //$path = $request->file('file')->store( storage_path('/storage/loonstrookjes/'));
            //$path = Storage::putFile(storage_path('/loonstrookjes/'), $filename);
            //dd($upload);

            //return $put;
        }

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

        return route('fileupload.create');
    }

}

But I'm getting a new error, this time it's:

Call to undefined method Illuminate\Support\Facades\File::hashName()

Any ideas??

  • 写回答

2条回答 默认 最新

  • dongxu4580 2019-03-22 22:06
    关注

    Your problem is you have a parameter in your method create(array $data), but you are posting the form using only {{ route('create') }}. Here you are calling the method by this route without passing the required parameter as you defined it.

    Basically, a form post method can accept the requested values by this

    protected function create(Request $request)

    Because you already used Request as a trait.

    So, by this, you can get the requested field value from your form. And you don't have to use $request = app('request'); since you already have it on the parameter variable $request.

    In case you want to know

    Variables are passed from frontend (view) to the backend (route) by using {{ route('update', $the_variable) }}.

    By this, you can have $the_variable after the last / of your route.

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题