I am working on an application for the company I work for, this application was made using Laravel 5.8 and MySQL. I have managed to incorporate improvements and so on but there is something that I still can't solve (I'm a junior programmer and I still don't have that much experience). The issue is that there is a many-to-many relationship, I need to make it possible to link new items to the project with their respective quantities in the edit view of a project, in turn it should be possible to unlink previously related items if needed. But nevertheless when trying to do it, the view responds to me with the following error: The items.0.id field is required.The items.0.quantity field is required.
Below the code:
ProjectController:
public function edit($id)
{
$project = Project::findOrFail($id);
$items = Item::all();
$project_items = $project->items()->pluck('id')->toArray();
$project_items_quantity = $project->items()->pluck('quantity', 'id')->toArray();
$item_project = ItemProject::where('project_id', $id)->get();
return view('includes.edit_delete_project',
compact('items', 'project_items', 'project_items_quantity', 'item_project'))
->with(['project'=> Project::getProjectById($id),
'entities'=>Entity::getEntities(),
'countries'=>Country::getCountries()]);
}
public function update(Request $request, Project $project)
{
$this->validate($request, array(
'code' => 'required|string|max:255',
'entity' => 'required|string|max:255',
'country' => 'required|string|max:255',
'items' => 'nullable|array',
'items.*.id' => 'required|integer|exists:items,id',
'items.*.quantity' => 'required|integer|min:0',
));
$project->update($request->only(['code', 'entity', 'country']));
if ($request->has('items')) {
$items = $request->input('items');
$currentItems = $project->items->pluck('id')->toArray();
$detachItems = array_diff($currentItems, array_column($items, 'id'));
$project->items()->detach($detachItems);
foreach ($items as $item) {
$project->items()->syncWithoutDetaching([$item['id'] => ['quantity' => $item['quantity']]]);
}
}
return response()->json($project);
}
Project Model:
class Project extends Model
{
protected $table = "project";
protected $fillable = ['code',
'entity',
'country'];
protected $hidden = ['id'];
public static function getProjects()
{
return Project::all();
}
public static function getProjectById($id)
{
return Project::find($id);
}
public function items()
{
return $this->belongsToMany(Item::class, 'project_item',
'project_id', 'item_id')->withPivot('quantity');
}
}
Project edit view:
<!-- Edit -->
<div class="modal fade" id="edit">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title"><b><span class="employee_id">Edit Project</span></b></h4>
</div>
<div class="modal-body">
<form class="form-horizontal" method="POST" action="/includes/edit_delete_project/">
@csrf
<div class="form-group">
<label for="code" class="col-sm-3 control-label"><span style="color: red">*</span> Code</label>
<div class="col-sm-9">
<input oninput="this.value = this.value.toUpperCase()" type="text" class="form-control"
id="code" name="code" value="" required>
</div>
</div>
<div class="form-group">
<label for="entity" class="col-sm-3 control-label"><span style="color: red">*</span> Company</label>
<div class="col-sm-9">
<select class="form-control" id="entity" name="entity" required>
<option value="" selected></option>
@foreach($entities as $entity)
<option value=""> </option>
@endforeach
</select>
</div>
</div>
<div class="form-group">
<label for="country" class="col-sm-3 control-label"><span style="color: red">*</span> Country</label>
<div class="col-sm-9">
<select class="form-control" id="country" name="country" required>
<option value="" selected></option>
@foreach($countries as $country)
<option value=""> </option>
@endforeach
</select>
</div>
</div>
<div class="form-group">
<label for="items" class="col-sm-3 control-label"><span style="color: red"></span> Items</label>
<div class="col-sm-9">
@foreach($items as $item)
<div class="form-check">
<input type="checkbox" class="form-check-input" name="items[]" value="" id="item">
<label class="form-check-label" for="item"> - </label>
<input type="number"
class="form-control"
min="1"
style="width: 100px;"
id="quantity_"
name="quantity_"
value="" placeholder="Quantity">
</div>
@endforeach
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default btn-flat pull-left" data-dismiss="modal"><i
class="fa fa-close"></i> Close
</button>
<button type="submit" class="btn btn-success btn-flat" name="edit"><i class="fa fa-check-square-o"></i>
Update
</button>
</div>
</form>
</div>
</div>
</div>
</div>
I have tried various recommendations from the internet but still have not been successful with any. It is the only thing that I need of all that they asked me to finish the application. I just need to solve what I exposed at the beginning and I will be very grateful to anyone who can help me.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire