Как создать несколько загружаемых файлов с 2 входными файлами

#php #laravel #laravel-7

#php #laravel #laravel-7

Вопрос:

Я создам несколько загружаемых файлов с 2 входными файлами, но я не знаю

Fields.blade.php

     <div class="form-group col-sm-6" id="cover">
     {!! Form::label('cover', 'Cover:') !!}
     {!! Form::file('cover', null, ['class' => 'form-control']) !!}
    </div>
    <div class="form-group col-sm-6" id="full">
     {!! Form::label('full', 'Full:') !!}
     {!! Form::file('full', null, ['class' => 'form-control']) !!}
    </div>
  

CatalogController.php

 public function createWithCategory($id)
{
    $katalog_metadata = KatalogMetadata::with('metadata')->where('category_id', $id)->get();
    return view('catalogs.create')->with('katalog_metadata', $katalog_metadata)->with('category_id', $id);
}
public function store(CreateCatalogRequest $request)
{
    $input = $request->all();
    if (Auth::user()->can('isAdmin')) {
        $input['status'] = 1;
    } else {
        $input['status'] = 0;
    }
    $input['cover'] = $this->uploadingCover($request);
    $input['full'] = $this->uploadingFull($request);
    $catalog = $this->catalogRepository->create($input);
    foreach ($input['metadata'] as $key => $value) {
        $val = [
            'metadata_id' => $key,
            'metadata_key' => $value['key'],
            'value' => $value['value'],
            'catalog_id' => $catalog->id
        ];
        $data = new CatalogMetadataValue($val);
        $catalog->catalog_metadata_value()->save($data);
    }
    if (!Auth::user()->can('isAdmin')) {
        $admin = User::where('role_id', '1')->first();
        Mail::to($admin->email)->send(new NotifyNewCatalog($catalog));
    }
    Flash::success('Catalog saved successfully.');
    return redirect(route('catalogs.index_with_category', $request->category_id));
}
protected function uploadingCover($request)
{
    $destinationPath = 'catalog/cover';
    if (!is_dir($destinationPath)) {
        if (!is_dir('catalog')) {
            mkdir('catalog');
        }
        mkdir($destinationPath);
    }
    if ($request->hasFile('cover')) {
        $file = $request->file('cover');
        $fileName = time() . '.' . $file->getClientOriginalExtension();
        $file->move($destinationPath, $fileName);
        return $destinationPath . '/' . $fileName;
    }
    return;
}

protected function uploadingFull($request)
{
    $destinationPath = 'catalog/full';
    if (!is_dir($destinationPath)) {
        if (!is_dir('catalog')) {
            mkdir('catalog');
        }
        mkdir($destinationPath);
    }
    if ($request->hasFile('full')) {
        $file = $request->file('full');
        $fileName = time() . '.' . $file->getClientOriginalExtension();
        $file->move($destinationPath, $fileName);
        return $destinationPath . '/' . $fileName;
    }
    return;
}
  

Спасибо

Комментарии:

1. Добро пожаловать в SO… какую ошибку вы получаете.?

2. не ошибка, а idk, как это создать…

Ответ №1:

  1. Имя элемента файла должно быть в массиве и добавлять несколько ключевых слов.
     i.e {!! Form::file('cover[]', null, ['class' => 'form-control','multiple'])
 !!} 
  
  1. В контроллере выполните загрузку с данными массива
      if ($request->hasFile('cover')) {
        foreach($request->file('cover') as $file)
        {
            $fileName = time() . '.' . $file->getClientOriginalExtension();
            $file->move($destinationPath, $fileName);  
            $data[] = $name;  
        }
    }