Home > Back-end >  Laravel move row data from table to another table using button
Laravel move row data from table to another table using button

Time:02-05

Just to have an example. After I click the button it should move the row into another table in the database just like it is shown here and delete it afterwards. After I click the approve button the data row should transfer to another table in database. I really don't know how to start something like this in Laravel and I really can't find something related.

Refer to this image

Here's my view.blade.php

<table  id="dataTable" width="100%" cellspacing="0">
                                                    <thead>
                                                        <tr>
                                                            <th hidden>Id</th>
                                                            <th>Name</th>
                                                            <th>Equipment</th>
                                                            <th>Reservation Date</th>
                                                            <th>Room</th>
                                                            <th>Action</th>
                                                         </tr>
                                                    </thead>

                                                    <tbody>
                                                         @foreach ($app as $resdata)

                                                        <tr>
                                                            <td hidden>{{$resdata->id}}  </td>
                                                            <td>{{$resdata->name}} </td>
                                                            <td>{{$resdata->Name_item}}</td>
                                                            <td>{{$resdata->dt_item}}</td>
                                                            <td>{{$resdata->room_item}}  </td>
                                                            <td>
                                                            <form action="admin.reservations.store{{ $resdata->id}}" method="POST">
                                                            {{ csrf_field() }}
                                                            <button type="submit"  >Accept <i ></i></button></a>
                                                            </form>
                                                            <button type="button" >Cancel <i ></i></button>
                                                            </td>
                                                        </tr>

                                                     @endforeach
                                                    </tbody>
                                                </table>

Here's my reservation controller

 public function store($id)
    {

    $first = Reservation::find($id); //this will select the row with the given id

    //now save the data in the variables;
    $ab = $first->name;
    $cd = $first->Name_item;
    $ef = $first->dt_item;
    $gh = $first->room_item;
    $ij = $first->ldate_item;


    $second = new AcceptedReservation();
    $second->a_name = $ab;
    $second->a_nitem = $cd;
    $second->a_ditem = $ef;
    $second->a_ritem = $gh;
    $second->a_ldateitem = $ij;
    $second->save();

    //then return to your view or whatever you want to do
    return view('admin.reservations.index')->with('message','Reservation Accepted');

    }

Also when i clicking the button it always having error Too few arguments to function App\Http\Controllers\Admin\ReservationsController::store(), 0 passed in C:\laragon\www\ecmtech\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 1 expected

CodePudding user response:

keep the store function empty remove $id also .like this

public function store()
{
  // do nothing
} 

Create a new function like this in the controller

     public function acceptReservation(Request $request, $id)
        {
    
        $first = Reservation::find($id); //this will select the row with the given id
    
        //now save the data in the variables;
        $ab = $first->name;
        $cd = $first->Name_item;
        $ef = $first->dt_item;
        $gh = $first->room_item;
        $ij = $first->ldate_item;
    
    
        $second = new AcceptedReservation();
        $second->a_name = $ab;
        $second->a_nitem = $cd;
        $second->a_ditem = $ef;
        $second->a_ritem = $gh;
        $second->a_ldateitem = $ij;
        $second->save();
     /// IF YOU NEED TO REMOVE 1st TABLE VALUE    
  $first->delete();

        //then return to your view or whatever you want to do
        return redirect()->route('admin.reservations.index')->with('message','Reservation Accepted');
    
        }

now DEFINE The ROUTE Like this

Route::post('reservation-accept/{id}', [ReservationsController::class,'acceptReservation'])->name('reservation.accept');

Now where you are adding the accept button add it call it like this

<form action="{{route('admin.reservation.accept',$resdata->id)}}" method="POST">
  @csrf
  <button type="submit"  >Accept <i ></i></button></a>
</form>
  •  Tags:  
  • Related