I have two table one is users and the next one is students, the students table store the data of users based on their role. and the relation between user and student is user_id in students table.
And the form includes three inputs for text and one for excel file to import. I want to import the users by file and their data by inputs.
my form is like this:
<form method='post' action="{{ route('importExcel') }}" enctype="multipart/form-data">
@csrf
<div class="box-body row">
<div class="col-md-12">
<label for="faculty" class="col-md-12">???????</label>
<div class="form-group">
<select name='faculty' class="form-control" id="faculty">
<option value=""> ?????? ??????? </option>
@foreach($faculties as $faculty)
<option value="{{ $faculty->id }}" >{{ $faculty->name }}</option>
@endforeach
</select>
</div>
</div>
<div class="col-md-12">
<label for="department" class="col-md-12">?????????</label>
<div class="form-group">
<select class="form-control" name="department" id="department">
<option value="">?????? ?????????</option>
@foreach($departments as $department)
<option value="{{ $department->id }}">{{ $department->name }}</option>
@endforeach
</select>
</div>
</div>
<div class="col-md-12">
<label for="sesson" class="col-md-12">???</label>
<div class="form-group">
<select name="sesson" id="sesson" class="form-control">
<option value="">?????? ???</option>
<option value="spring">????</option>
<option value="fall">????</option>
</select>
</div>
</div>
<div class="col-md-12">
<label for="file" class="col-md-12">???? ????</label>
<div class="form-group">
<input type="file" name="file" class="form-control" id="file">
</div>
</div>
<input type="hidden" name="role" value="user">
<input type="hidden" name="admin" value="admin">
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-info pull-left submit">?????</button>
</div>
<!-- /.box-footer -->
</form>
and my import class is like this:
class UsersImport implements ToCollection
{
public function collection(Collection $rows)
{
foreach ($rows as $row)
{
User::create([
'name' => str_replace(' ', '_', $row['name']),
'email' => $row['email'],
'password' => bcrypt(str_replace(' ', '_', $row['name'])),
'role' => 'user',
]);
Student::create([
'user_id' => DB::table('users')->getPdo()->lastInsertId(),
'department_id' => $row['department'],
'faculty_id' => $row['faculty'],
'sesson' => $row['sesson']
]);
}
}
}
and my controller is like this:
try{
$this->validate($request,[
'faculty' => 'required',
'department'=> 'required',
'sesson'=> 'required',
'file'=> 'required',
]);
$file = $request->file('file');
$faculty = $request-> faculty;
$department = $request -> department;
$sesson = $request -> sesson;
Excel::import(new UsersImport,[$file, $faculty, $department, $sesson]);
return redirect()->route('admin.students')->with('success','?????? ??????? ????? ??.');
}
catch(Exception $e){
return redirect()->back()->with('error','?????? ????? ???!');
}
but its not working, the error is like this: pathinfo() expects parameter 1 to be string, array given
question from:
https://stackoverflow.com/questions/65642273/how-to-import-excel-file-into-two-tables-with-input-tags-at-the-same-time 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…