gpt4 book ai didi

laravel - 上传文件数组 Laravel

转载 作者:行者123 更新时间:2023-12-05 06:21:08 28 4
gpt4 key购买 nike

我想在 Laravel 中上传一组文件,但我不确定文件的路径和存储对象。八现在数据已存储,但在我的情况下路径是#。在下图中,我有从前面发送的数据(Vuejs 和我正在使用 vue-upload-component)

enter image description here

$fileName = [];

foreach($request->input('files') as $files){
$contractFile = new ContractFile();
$contractFile->fill([
'contract_id' => $contract->id,
'name' => $files['name'],
'path' => '#',
])->save();
}

契约(Contract)文件

class ContractFile extends Model
{
protected $fillable = ['path','contract_id','name'];

public function contract()
{
return $this->belongsTo(Contract::class);
}

}

合约文件数据库

    Schema::create('contract_files', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('contract_id');
$table->string('path');
$table->string('name');
$table->timestamps();
});

文件系统.php

'uploads' =>[
'driver' => 'local',
'root' => storage_path().'file/uploads',
],

最佳答案

你可以使用

foreach($request->file('files') as $uploadedFile){

$filename = time() . '_' . $uploadedFile->getClientOriginalName();

$path = $uploadedFile->store($filename, 'uploads');

$contractFile = new ContractFile();
$contractFile->contract_id = $contract->id;
$contractFile->name = $uploadedFile->getClientOriginalName();
$contractFile->path = $path;
$contractFile->save();

}

By default, the public disk uses the local driver and stores these files in storage/app/public. To make them accessible from the web, you should create a symbolic link from public/storage to storage/app/public.

创建 symbolic link您应该使用 storage:link Artisan 命令:

php artisan storage:link

关于laravel - 上传文件数组 Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60096685/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com