Home > Back-end >  Button in table row redirect to another page with the data of the row in Laravel
Button in table row redirect to another page with the data of the row in Laravel

Time:10-07

I would like to insert a button in every row of a table and when I click on this button, it redirect me to another page with the data of the single table row using Laravel How can I do this?

This is my form:

<html>
        <table class="table">
      <thead>
        <tr>
          <th scope="col">ID</th>
          <th scope="col">USERNAME</th>
          <th scope="col">MAC ADDRESS</th>
        </tr>
      </thead>
      <tbody>
        @foreach ($data as $item)
        <tr>
          <th scope="row">{{$item->id}}</th>
          <td>{{$item->username}}</td>
          <td>{{$item->mac_addr}}</td>
          <td>
          <form action="{{url('singleDevice')}}" method="get">
            <button class="btn btn-primary" type="submit">Select</button>
          </form>
        </td>
        </tr>
        @endforeach
      </tbody>
    </table>
    </body>
      
    </html>

This is my controller:

class DeviceController extends Controller
{
    public function index()
    {
        $data=Device::all();
        return view('device', compact("data"));
    }

    public function create()
    {
        return view('registrationDevice');
    }

    public function store(Request $request)
    {
        $input = new Device;
        //On left field name in DB and on right field name in Form/view
        $input -> username = $request->username;
        $input -> mac_addr = $request->mac_address;
        $input->save();

        return redirect('registrationDevice')->with('message', 'DATA SAVED');
    }

    public function show(Device $device)
    {
        
        return view('singleDevice');
    }
}

Thanks in advance

CodePudding user response:

Change your form like :

<tbody>
   @foreach ($data as $item)
     <tr>
      <th scope="row">{{$item->id}}</th>
       <td>{{$item->username}}</td>
       <td>{{$item->mac_addr}}</td>
       <td>
         <a href="{{ url('/singleDevice/'.$item->id) }}" class="btn btn-primary">Select</a>
       </td>
     </tr>
   @endforeach
</tbody>

If you want to use route name, you can change by :

<td>
  <a href="{{ route('show', ['deviceID' => $item->id]) }}" class="btn btn-primary">Select</a>
</td>

Change your route like :

Route::get('/singleDevice/{deviceID}', [DeviceController::class, 'show'])->name('show');

Change your show function of the controller like:

public function show($deviceID)
 {
   $device = Device::firstWhere('id', $deviceID);
   return view('singleDevice', compact("device"));
 }

CodePudding user response:

You also can use this  as you have already used form in your code

   <form action="{{ route('show',  $item->id) }}" method="get">
    <button class="btn btn-primary" type="submit">Select</button>
    </form>
 

public function show(Device $device)
    {
        
        return view('singleDevice');
    }


For Route you can pass $device:
Route::get('/singleDevice/{$device}', [DeviceController::class, 'show'])->name('show');

CodePudding user response:

Why do you need a form? use the link

<form action="{{url('singleDevice')}}" method="get">
    <button class="btn btn-primary" type="submit">Select</button>
</form>

replace with

<a href="{{ route('show', ['device' => $item->id]) }}" class="btn btn-primary">Select</a>

and configure the URL in the router as /singleDevice/{device}

Route::get('/singleDevice/{device}', [DeviceController::class, 'show'])->name('show');
  • Related