В моем Laravel -5.8 проекте, состоящем из приложения Sales, у меня есть следующие коды:
Контроллер:
public function create()
{
$suppliers =Supplier::all();
$categories = Category::all();
$taxes = Tax::all();
$units = Unit::all();
return view('product.create', compact('categories','taxes','units','suppliers'));
}
public function store(Request $request)
{
$request->validate([
'name' => 'required|min:3|unique:products|regex:/^[a-zA-Z ]+$/',
'serial_number' => 'required',
'model' => 'required|min:3',
'category_id' => 'required',
'sales_price' => 'required',
'unit_id' => 'required',
'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'tax_id' => 'required',
]);
$product = new Product();
$product->name = $request->name;
$product->serial_number = $request->serial_number;
$product->model = $request->model;
$product->category_id = $request->category_id;
$product->sales_price = $request->sales_price;
$product->unit_id = $request->unit_id;
$product->tax_id = $request->tax_id;
if ($request->hasFile('image')){
$imageName =request()->image->getClientOriginalName();
request()->image->move(public_path('images/product/'), $imageName);
$product->image = $imageName;
}
$product->save();
foreach($request->supplier_id as $key => $supplier_id){
$supplier = new ProductSupplier();
$supplier->product_id = $product->id;
$supplier->supplier_id = $request->supplier_id[$key];
$supplier->price = $request->supplier_price[$key];
$supplier->save();
}
return redirect()->back()->with('message', 'Product Created Successfully');
}
view
<div class="row mt-2">
<div class="clearix"></div>
<div class="col-md-10">
<div class="tile">
<h3 class="tile-title">Product</h3>
<div class="tile-body">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form method="POST" action="{{route('product.store')}}" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="form-group col-md-6">
<label class="control-label">Product Name</label>
<input name="name" class="form-control @error('name') is-invalid @enderror" type="text" placeholder="Product Name">
@error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Serial Number</label>
<input name="serial_number" class="form-control @error('serial_number') is-invalid @enderror" type="number" placeholder="Enter Tax Name">
@error('serial_number')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Model</label>
<input name="model" class="form-control @error('name') is-invalid @enderror" type="text" placeholder="Enter Tax Name">
@error('model')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Category</label>
<select name="category_id" class="form-control">
<option>---Select Category---</option>
@foreach($categories as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
@endforeach
</select>
@error('category_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Sale Price</label>
<input name="sales_price" class="form-control @error('sales_price') is-invalid @enderror" type="number" placeholder="Enter Tax Name">
@error('sales_price')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Unite</label>
<select name="unit_id" class="form-control">
<option>---Select Unit---</option>
@foreach($units as $unit)
<option value="{{$unit->id}}">{{$unit->name}}</option>
@endforeach
</select>
@error('unit_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Image</label>
<input name="image" class="form-control @error('image') is-invalid @enderror" type="file" >
@error('image')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Tax </label>
<select name="tax_id" class="form-control">
<option>---Select Tax---</option>
@foreach($taxes as $tax)
<option value="{{$tax->id}}">{{$tax->name}} %</option>
@endforeach
</select>
@error('tax_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="tile">
<div id="example-2" class="content">
<div class="group row">
<div class="form-group col-md-5">
<select name="supplier_id[]" class="form-control">
<option>Select Supplier</option>
@foreach($suppliers as $supplier)
<option value="{{$supplier->id}}">{{$supplier->name}} </option>
@endforeach
</select>
</div>
<div class="form-group col-md-5">
<input name="supplier_price[]" value="{{old('supplier_price')}}" class="form-control @error('supplier_price') is-invalid @enderror" type="number" placeholder="Enter Sales Price">
<span class="text-danger">{{ $errors->has('additional_body') ? $errors->first('body') : '' }}</span>
</div>
<div class="form-group col-md-2">
<button type="button" id="btnAdd-2" class="btn btn-primary float-right"><i class="fa fa-plus"></i></button>
<button type="button" class="btn btn-danger btnRemove float-right"><i class="fa fa-trash"></i></button>
</div>
</div>
</div>
</div>
<div class="form-group col-md-4 align-self-end">
<button class="btn btn-primary" type="submit"><i class="fa fa-fw fa-lg fa-check-circle"></i>Update</button>
</div>
</form>
</div>
</div>
</div>
</div>
Когда я отправив форму, я получил эту ошибку:
htmlspecialchars () ожидает, что параметр 1 будет строкой, задан массив (View: ...
Затем, когда я удалил это :
value = "{{old ('supplier_price')}}"
from:
<div class="form-group col-md-5">
<input name="supplier_price[]" value="{{old('supplier_price')}}" class="form-control @error('supplier_price') is-invalid @enderror" type="number" placeholder="Enter Sales Price">
<span class="text-danger">{{ $errors->has('additional_body') ? $errors->first('body') : '' }}</span>
</div>
ошибка исчезла.
Я хотел использовать его для поля ввода, чтобы сохранить его значение после любой ошибки при проверке. Где я понял ошибку и как ее исправить?
Спасибо