Linking edit button from table to Laravel Edit form

12,833

Solution 1

Try this:

<button href="computers/{id}/edit">{{ trans('computers.edit') }}</button>

Or you could use form (Laravel Collective way):

{!! Form::open(['method' => 'Get', 'route' => ['computers.edit', $inventory->id]]) !!}
{!! Form::submit(trans('computers.edit')) !!}
{!! Form::close() !!}

Solution 2

Instead of using a button I would use an <a> tag.

<a href="{{ url('computers/'.$inventory->id.'/edit') }}>{{ trans('computers.edit') }}</a>

the url() function is a Laravel helper function

Also.. I'm sure there are enough examples of things like this, so make sure you google your question first.

Share:
12,833
Afonte
Author by

Afonte

Updated on June 18, 2022

Comments

  • Afonte
    Afonte almost 2 years

    I have a table that displays the data from my database. at the end of each row I have an Edit/Update Button. I would like it when clicking on the edit button it reference the the Edit Form.

    My edit form works. I can access the data when visiting computers/{id}/edit, The form displays the current data and I can edit the data and submit the updates and it updates in the database (mysql).

    This is my index.blade.php, which displays the table with the update button

    @extends('layout')
    
    
    @section('content')
        <h1>Inventory</h1>
    
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>Last Name</th>
                        <th>First Name</th>
                        <th>Department</th>
                        <th>Building</th>
                        <th>Room</th>
                        <th>Manufacturer</th>
                        <th>Device</th>
                        <th>Model</th>
                        <th>Service Tag</th>
                        <th>Mac Address</th>
                        <th>Status</th>
                        <th>Comments</th>
    
    
                    </tr>
                </thead>
    
                <tbody>
    
                    @foreach($inventories as $inventory)
                        <tr>
                        <td>{{$inventory->lastName}}</td>
                        <td>{{$inventory->firstName}}</td>
                        <td>{{$inventory->department}}</td>
                        <td>{{$inventory->building}}</td>
                        <td>{{$inventory->room}}</td>
                        <td>{{$inventory->manufacturer}}</td>
                        <td>{{$inventory->device}}</td>
                        <td>{{$inventory->model}}</td>
                        <td>{{$inventory->tag}}</td>
                        <td>{{$inventory->macAddress}}</td>
                        <td>{{$inventory->status}}</td>
                        <td>{{$inventory->comments}}</td>
                        <td>
                            {{--Need the button to open up my edit form--}}
                            <button formaction="computers/{id}/edit">{{ trans('computers.edit') }}</button>
                            {{--<input type="submit" name="update" id="update" value="Update" class="btn btn-primary">--}}
                        </td>
                </tr>
                    @endforeach
                </tbody>
            </table>
    
    
    @stop
    

    This is my form.blade.php - which is a partial that I include in my create.blade.php and edit.blade.php and both of these pages work.

    <div class="row">
        <div class="col-md-6">
    
    
    
            <div class="form-group">
                {!! Form::label('lastName', 'Last Name:') !!}
                {!! Form::text('lastName', null, ['class' => 'form-control' ]) !!}
    </div>
    
    <div class="form-group">
        {!! Form::label('firstName', 'First Name:') !!}
        {!! Form::text('firstName', null, ['class' => 'form-control'])  !!}
    
    </div>
    
    <div class="form-group">
        {!! Form::label('departmen', 'Department:') !!}
        {!! Form::text('department', null, ['class' => 'form-control'])  !!}
    
    </div>
    
    <div class="form-group" >
        {!! Form::label('building', 'Building:') !!}
        {!!  Form::select('building', ['vanHall' => 'Vanderbilt Hal',
                    'wilf' => 'Wilf Hall',
                    'dag' => 'D Agostino Hall',
                    'furmanHall' => 'Furman Hall',
                    'wsn' => 'WSN',
                    'mercer' => 'Mercer',
                    'training' => 'Traing Room',
                    'storage' => 'Storage'
    
    </div>
    
    <div class="form-group">
        {!! Form::label('room', 'Room:') !!}
        {!! Form::text('room', null, ['class' => 'form-control'])  !!}
    
    
    </div>
    
    <div class="form-group">
        {!! Form::label('manufacturer', 'Manufacturer:') !!}
        {!! Form::text('manufacturer', null, ['class' => 'form-control'])  !!}
    
    </div>
    
        </div>
    
        <div class="col-md-6">
    <div class="form-group">
        {!! Form::label('device', 'Device:') !!}
                    {!!  Form::select('device', ['desktop' => 'Desktop',
                    'laptop' => 'Laptop',
                    'classroom' => 'Classroom',
                    'printer' => 'Printer',
                    'mifi' => 'MiFi',
                    'panopto' => 'Panopto',
                    'Other' => 'Other',
                     ], null, ['placeholder' => 'Select Device'])!!}
    
    </div>
    
            <div class="form-group">
                {!! Form::label('model', 'Model:') !!}
                {!! Form::text('model', null, ['class' => 'form-control'])  !!}
    
          </div>
    
          <div class="form-group">
              {!! Form::label('tag', 'Service Tag:') !!}
              {!! Form::text('tag', null, ['class' => 'form-control'])  !!}
    
          </div>
    
          <div class="form-group">
              {!! Form::label('macAddress', 'Mac Address:') !!}
              {!! Form::text('macAddress', null, ['class' => 'form-control'])  !!}
    
          </div>
    
            <div class="form-group">
                {!! Form::label('status', 'Status:') !!}
                {!!  Form::select('status', ['active' => 'Active',
                'inactive' => 'Inactive',
                 ], null, ['placeholder' => 'Status'])!!}
    
    
          </div>
    
    
    
          <div class="form-group">
              {!! Form::label('comments', 'Comments:') !!}
              {!! Form::text('comments', null, ['class' => 'form-control'])  !!}
          </div>
        </div>
    
       <div class="col-md-12">
        <hr>
    <div class="form-group">
    
        {!! Form::submit($submitButtonText, ['class' => 'btn btn-primary form-control']) !!}
        {{--<button type="submit" class="btn btn-primary">Submit</button>--}}
    
    </div>
    
    </div>
    
  • Afonte
    Afonte over 7 years
    I had tried this before but the button does change and says computer.edit but it does not open the edit form.
  • Afonte
    Afonte over 7 years
    The form Collective Worked. The href didn't and I tried multiple ways to do this. Thank you so much!