'Show row validation message in blade file for Laravel Excel data - maatwebsite-excel
What I'm trying to acheive?
Validate the records available in the
csv filethat I have uploaded usingmaatwebsite/excel.Set custom validation rules:
i.e.
contact_number to be integer and 13 digits
email to be strictly email etc
If all the csv file data is valid then upload in database and show
message.If certain feild of the table is missing or not according to custom validation rules set above then show those data rows of the csv file in the blade and give user the option to update the feild and upload that data to DB.
What I'm doing?
Using Laravel Excel, I'm importing the data. I am not quite sure how the back end works.
public function import(Request $request)
{
Excel::import(new CustomersImport, $request->file);
return back()->with('success', 'User Imported Successfully.');
}
What Row Validation is showing is that they set the rules inside rules() function inside the import class and create model with table feilds. Tbh I really don't know how csv data is being transferred here. Data is also not uploading in database.
Whwn I enter dd($row) inside the model(array $row) it shows the recod of only first row of the table.
class CustomersImport implements ToModel, WithHeadingRow
{
use Importable;
public function rules(): array
{
return [
'name' => 'required|string',
'email' => 'required|string',
];
}
public function model(array $row)
{
return new Customer([
"name" => $row['name'],
"email" => $row['email'],
"contact" => $row['contact'],
"address" => $row['address'],
"nutritionplan_id" => $row['nutritionplan_id'],
"company_id" => $row['company_id'],
]);
}
}
blade file. I actuality, I want I want to show table with records that are not validated if it have errors. But for now I'm just trying to show error.
@if (count($errors) > 0)
<div class="row">
<div class="col-md-8 col-md-offset-1">
<div class="alert alert-danger alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-ban"></i> Error!</h4>
@foreach($errors->all() as $error)
{{ $error }} <br>
@endforeach
</div>
</div>
</div>
@endif
@if (Session::has('success'))
<div class="row">
<div class="col-md-8 col-md-offset-1">
<div class="alert alert-success alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h5>{!! Session::get('success') !!}</h5>
</div>
</div>
</div>
@endif
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
