Я использую 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