【发布时间】:2019-06-18 09:41:01
【问题描述】:
我在我的网络应用程序中使用 Laravel,我想用他自己的形式将文件与我的帖子以独立的方式关联,但我遇到了一些问题
我的路线(我正在使用身份验证控制包,但实际上我是管理员):
Route::post('file', 'fileController@store')->name('file.store')
->middleware('permission:file.create');
Route::get('file', 'fileController@index')->name('file.index')
->middleware('permission:file.index');
Route::get('file/create/', 'fileController@create')->name('file.create')
->middleware('permission:file.create');
Route::put('file/{id}', 'fileController@update')->name('file.update')
->middleware('permission:file.edit');
Route::get('file/{id}', 'fileController@show')->name('file.show')
->middleware('permission:file.show');
Route::delete('file/{id}', 'fileController@destroy')->name('file.destroy')
->middleware('permission:file.destroy');
Route::get('file/{id}/edit', 'fileController@edit')->name('file.edit')
->middleware('permission:file.edit');
Route::get('download/{filename}', 'fileController@download')->name('file.download')
->middleware('permission:file.download');
我的迁移:
Schema::create('files', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('files_id')->unsigned();
$table->string('filenames');
$table->integer('fileable_id')->unsigned();
$table->string('fileable_type');
$table->timestamps();
});
我的文件模型:
class File extends Model
{
protected $fillable = [
'filenames', 'project_id'
];
public function user()
{
return $this->belongsTo(User::class);
}
我的项目模型:
public function files()
{
return $this->morphMany(File::class, 'fileable')->whereNull('files_id');
}
要存储的控制器:
class FileController extends Controller
{
public function store(Request $request)
{
$this->validate($request, [
'filenames' => 'required',
'project_id' => 'required',
// 'filenames.*' => 'mimes:doc,pdf,docx,zip'
]);
if($request->hasfile('filenames'))
{
foreach($request->file('filenames') as $file)
{
$name=$file->getClientOriginalName();
$file->move(public_path().'/files/', $name);
$data[] = $name;
}
}
$file= new File();
$file->filenames = $request->get('filenames');
$file->filenames= $name;
$file->user()->associate($request->user());
$project = Project::findOrFail($request->get('project_id'));
$project->files()->save($file);
$file->save();
return back();
}
public function download( $filename = '' ) {
// Check if file exists in storage directory
$file_path = public_path() . '/files/' . $filename;
if ( file_exists( $file_path ) ) {
// Send Download
return \Response::download( $file_path, $filename );
} else {
return back()->with('info', 'Archivo no existe en el servidor');
}
}
刀片中的表单:
<form method="post" action="{{ route('file.store') }}" enctype="multipart/form-data">
<div class="input-group hdtuto control-group lst increment" >
<input type="file" name="filenames[]" class="myfrm form-control">
<input type="hidden" name="project_id" value="{{ $project->id }}" />
<div class="input-group-btn">
<button class="btn btn-success" type="button"><i class="fldemo glyphicon glyphicon-plus"></i>Add</button>
</div>
</div>
<button type="submit" class="btn btn-success" style="margin-top:10px">Submit</button>
</form>
Foreach 下载文件:
@foreach($project->files as $file)
<li>{{ $file->user->name }}: <a href="{{ url('/download/')}}/{{$file->filenames}}" download> {{$file->filenames}}</a></li>
@endforeach
我从 Project Controll 发送文件
【问题讨论】:
-
$project = Project::find($request->get('project_id'));如果返回的是null,则不能调用$project->files() ...。检查$request->get('project_id')包含的内容,并确保您拥有该 ID 的Project。
标签: php html laravel file-upload