How do i update the database via options value in HTML ? i made it manually, in the examples below but i want it to update dinamically so i think i need to change something here but i don t know how.(example below) . database table name = ‘proiecte’, status_proiect = project status, contract semnat = contract signed.
ShowController.php
public function update(Request $request, Proiecte $proiecte)
{
$proiecte->update($request->all());
Proiecte::find($request->id)->update(['Status_Proiect' => 'Contract semnat']); <- **HERE I WANT TO MAKE IT DINAMICALLY**
Proiecte::find($request->id)->save();
// dd(Proiecte::find($request->id));
return back();
}
my home.blade.php
@foreach($data as $data)
<tr class="table-row-data">
<td>{{ $data->id }}</td>
<td>{{ $data->Denumire_Proiect }}</td>
<td>{{ $data->Firma_Client }}</td>
<td>{{ $data->Reprezentant_Firma }}</td>
<td>{{ $data->Contact_Client }}</td>
<td>{{ $data->Suma_Proiect }}</td>
<td>{{ $data->Numar_Transe }}</td>
<td>{{ $data->Status_Proiect }}</td>
<td>
{{-- <form action='home' method="POST"> --}}
<form action="{{ route('update') }}" method="POST">
@csrf
@method('put')
<select name="id">
<option value="{{ $data->id }}" name="pil">Proiect in lucru</option>
<option value="{{ $data->id }}" name="cs">Contract semnat</option>
<option value="{{ $data->id }}" name="pf">Proiect finalizat</option>
</select>
<button type="submit" class="btn btn-primary">Schimbare</button>
</form>
</td>
</tr>
@endforeach
and now is the interesting part because i don t know how to update dinamically using because i don t know how to get the information from the site when i select an option and i don t know how to write in the controller update function.
web.php
Route::put('/home', [AppHttpControllersShowController::class, 'update'])->name('update');
i made it to work but not dinamically, but manually. if i enter any value where i said "HERE I WANT TO MAKE IT DINAMICALLY" it works, it updates my database when i press the btn "schimbare" (change btn)
3
Answers
below code update all request parameters.
$proiecte->update($request->all());
when you submit a form a array of field name as key and value as value will be send to server. the
$request->all()
contains this array.<option>
tag not needname
attribute . you must a name attribute to select tag and option tags have value attribute as value of select namethe
$request->all()
looks like this:then you can update parameter
The problem is that you are passing the value of
$data->id
to each option incorrectly:The database doesn’t know which of the options (pil, cs, pf) to select based on this.
The value should be something unique for each option, you can either do it by ID’s, or by string
You already defined that the select statement name will be "id" here
<select name="id">
so the request passed is passing the value forrequest()->id
based on it, you can also always dodd(request()->all())
to check what is actually being passed to your controllerFor updating the request, you can do:
Probably you should change the way the names of things are to not be confusing.
To update the field, you need an input (select in thins case) and another input to keep the id
Then in your controller you use the two inputs
id
andStatus_Proiect
to update your entry2nd option using model binding
If you want to use Model binding, you need to change the route, form action and your update method
Form (you dont need the hidden input for the id anymore)
Update method (you dont need to get the $projecte with find anymore)