skip to Main Content

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.

image here
img2 here

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


  1. 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.

    
    <form action="{{ route('update') }}" method="POST">
            @csrf
            @method('put')
            <select name="status">
              <option value="pil">Proiect in lucru</option>
              <option value="cs" >Contract semnat</option>
              <option value="pf" >Proiect finalizat</option>
            </select>
            <button type="submit" class="btn btn-primary">Schimbare</button>
    </form>
    
    

    <option> tag not need name attribute . you must a name attribute to select tag and option tags have value attribute as value of select name

    the $request->all() looks like this:

    [
    "status"=>"pil"
    ]
    

    then you can update parameter

    $proiecte->update([
    'status'=>$request->input('status')
    ]);
    
    Login or Signup to reply.
  2. The problem is that you are passing the value of $data->id to each option incorrectly:

                    <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>
    

    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

               //Example 1
               <select name="id">
                    <option value="0">Proiect in lucru</option>
                    <option value="1">Contract semnat</option>
                    <option value="2">Proiect finalizat</option>
                </select>
    
                //Example 2
                <select name="id">
                    <option value="pil">Proiect in lucru</option>
                    <option value="cs">Contract semnat</option>
                    <option value="pf">Proiect finalizat</option>
                </select>
    

    You already defined that the select statement name will be "id" here <select name="id"> so the request passed is passing the value for request()->id based on it, you can also always do

    dd(request()->all()) to check what is actually being passed to your controller

    For updating the request, you can do:

    $proiecte->update(['Status_Proiect' => request()->id]);
    

    Probably you should change the way the names of things are to not be confusing.

    Login or Signup to reply.
  3. To update the field, you need an input (select in thins case) and another input to keep the id

    @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="{{ route('update') }}" method="POST">
                    @csrf
                    @method('put')
                    <input type="hidden" name="id" value="{{ $data->id }}"/>
                    <select name="Status_Proiect">
                        <option value="Proiect in lucru" {{$data->Status_Proiect == 'Proiect in lucru' ? 'selected':''}}>Proiect in lucru</option>
                        <option value="Contract semnat" {{$data->Status_Proiect == 'Contract semnat' ? 'selected':''}}>Contract semnat</option>
                        <option value="Proiect finalizat" {{$data->Status_Proiect == 'Proiect finalizat' ? 'selected':''}}>Proiect finalizat</option>
                    </select>
                    <button type="submit" class="btn btn-primary">Schimbare</button>
                </form>
            </td>
        </tr>
    @endforeach
    

    Then in your controller you use the two inputs id and Status_Proiect to update your entry

    public function update(Request $request)
    {
        
        $projecte = Proiecte::findOrFail($request->id);
        $projecte->Status_Proiect = $request->Status_Proiect;
        $projecte->save();
        // dd(Proiecte::find($request->id));
        return back();
    }
    

    2nd option using model binding

    If you want to use Model binding, you need to change the route, form action and your update method

    Route::put('/home/{projecte}', [AppHttpControllersShowController::class, 'update'])->name('update');
    

    Form (you dont need the hidden input for the id anymore)

    <form action="{{ route('update', ['projecte' => $data->id]) }}" method="POST">
                    @csrf
                    @method('put')
                    <select name="Status_Proiect">
                        <option value="Proiect in lucru" {{$data->Status_Proiect == 'Proiect in lucru' ? 'selected':''}}>Proiect in lucru</option>
                        <option value="Contract semnat" {{$data->Status_Proiect == 'Contract semnat' ? 'selected':''}}>Contract semnat</option>
                        <option value="Proiect finalizat" {{$data->Status_Proiect == 'Proiect finalizat' ? 'selected':''}}>Proiect finalizat</option>
                    </select>
                    <button type="submit" class="btn btn-primary">Schimbare</button>
                </form>
    

    Update method (you dont need to get the $projecte with find anymore)

    public function update(Request $request, Proiecte $proiecte)
    {
        $projecte->Status_Proiect = $request->Status_Proiect;
        $projecte->save();
        // dd(Proiecte::find($request->id));
        return back();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search