将具有用户ID的文件上载到数据库

时间:2017-07-13 12:46:36

标签: php laravel

大家好我想添加一个文件,而不是图像。 laravel中有不同类型的文件。使用该文件,我需要将用户ID上传到数据库,但我不知道如何将其存储在控制器中,这是我的脚本:

Controller.php这样:

public function uploaded(Request $request, $id)
    {
        $client = Client::findOrFail($id);

         $path = $request->image->store('storage/uploads','public');

        return back();
    }

upload.blade.php

{!! Form::open(['action' => ['FileController@upload', $client->id, 'files' => 'true']]) !!}
                    {!!Form::file('image') !!}
                    {!!Form::submit('Upload File') !!}
                   {!! Form::close() !!}

和web.php

Route::post('upload/{id}/', 'FileController@uploaded');

你能解释一下如何正确地做到这一点吗?

2 个答案:

答案 0 :(得分:0)

只需将upload.blade.php更改为

即可
{!! Form::open(['route' => ['file.upload', $client->id, 'files' => 'true']]) !!}
    {!!Form::file('image') !!}
    {!!Form::submit('Upload File') !!}
{!! Form::close() !!}

和您的web.php

Route::post('upload/{id}/', 'FileController@uploaded')->name('file.upload');

答案 1 :(得分:0)

我对你的问题很不以为然。但我希望我的回答可以帮助你解决问题。

第一回答:

如果你想将文件保存在数据库中,我认为你可以保存文件路径,只保存在另一个表中,我们可以用这种结构称它为user_files

<强> user_files

| id | user_id |文件|

| 1 | 2 | /家庭/名称/文件

此表与clients表之间的关系是oneToMany。

所以在你的Client.php中将有这个方法:

<强> Client.php

public function files() {
    return $this->hasMany(File::class, 'user_id');
}

你的File.php将有这个方法:

<强> File.php

public function client() {
    return $this()->belongsTo(Client::class, 'user_id');
}

之后,现在我们进入处理上传方法的控制器。我们应该保存文件并关联来自客户端的数据。

public function uploaded(Request $request, $id)
{
    $client = Client::findOrFail($id);

    // If you need to upload file not only image, I think you should use $request->file() method;
    $file = $request->file('file');


    // And then we save the name use this method, maybe you want to save it with change the name and include the user_id
    $name = 'User' . '#' . $client->id . '.' . $file->getClientOriginalExtension(); 
    // It will make the file named 'User#2.doc'

    // After that we move the file in 'uploads' directory or other public directory you want.
    $file->move(public_path('uploads'), $name);

    $newFile = new File;
    $newFile->file = public_path('uploads') . '/' . $name;
    $newFile->client()->associate($client);
    $newFile->save();


    return back();
}

如果你想用user_id作为标识符保存数据库中的文件,你也可以照常访问它,就像你想要访问图像文件一样:

<img src="{{$newFile->file}}" />

第二次回答

但是如果您只想保存名为user_id的文件,则只能使用控制器中的方法:

public function uploaded(Request $request, $id)
{
    $client = Client::findOrFail($id);

    // If you need to upload file not only image, I think you should use $request->file() method;
    $file = $request->file('file');


    // And then we save the name use this method, maybe you want to save it with change the name and include the user_id
    $name = 'User' . '#' . $client->id . '.' . $file->getClientOriginalExtension(); 
    // It will make the file named 'User#2.doc'

    // After that we move the file in 'uploads' directory or other public directory you want.
    $file->move(public_path('uploads'), $name);

    return back();
}