0

I have a problem when import data xlsx using package fast excel. I wanna input excel files include the Id from different model like following below

app/http/clustercontroller :

public function Import($id)
{
    $model = Cluster::findOrFail($id);

    return view('components.Admin.import', compact('model'));      

}

public function StoreImport($id, Request $request)
{

    //VALIDASI
    $this->validate($request, [
        'file' => 'required|mimes:xls,xlsx',
    ]);

    if ($request->hasFile('file')) {
        $file = $request->file('file'); //Get File
        $collection = (new FastExcel)->import($file, function ($line) use ($id) {
            return Soal::create([
                'soal' => $line['Soal'],
                'image' => $line['Image'],
                'A' => $line['A'],
                'B' => $line['B'],
                'C' => $line['C'],
                'D' => $line['D'],
                'E' => $line['E'],
                'kunci' => $line['Kunci'],
                'cluster_id' => $id
            ]); //Import File
        }); 

    }
}

resource/admin/import.blade.php :

{!! Form::model($model, [
    'route' => $model->exists ? ['cluster.soal.store', $model->id] : 'cluster.soal.create',
    'method' => $model->exists ? 'POST' : 'POST',
    'files' => true
]) !!}


    <div class="form-group">
        <label for="" class="control-label">Cluster</label>
        {!! Form::text('cluster', null, ['class' => 'form-control', 'id' => 'cluster']) !!}
    </div>

    <div class="form-group">
        <label for="" class="control-label">File .xlsx</label>
        {!! Form::file('files') !!}
    </div>

{!! Form::close() !!}

the code above displays the form, but when I click submit there is no response

tasyadas
  • 43
  • 6

1 Answers1

0

You don't seem to return a response in your Controller method, so that's one reason I can think of. I assume your models are stored?

Things you could do to improve:

  • Return a view or, even better, redirection after finishing the import
  • Surround your code with try/catch blocks
Analogue
  • 87
  • 5