Linking edit button from table to Laravel Edit form

时间:2016-10-04 14:43:42

标签: forms laravel laravel-5 laravel-5.2

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>

3 个答案:

答案 0 :(得分:1)

我不使用按钮而是使用<a>标签。

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

url()函数是Laravel helper function

此外..我确信有足够的例子可以说明这一点,所以请务必先查看你的问题。

答案 1 :(得分:0)

试试这个:

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

或者您可以使用表格(Laravel Collective方式):

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

答案 2 :(得分:0)

对不起,我晚答复

如果您要创建bigger project,并且不想在每个{p

write the code文件,用于生成index.balde.php按钮

只需使用我的助手功能

例如:

Laravel 5.7 打开您的模型,它可能是Edit,Show,DeleteComputer

只需将以下代码粘贴到其中

SomeModel

现在全部设置完毕,然后打开index.blade.php

并替换您的代码

public static  function tableActionButtons($fullUrl,$id,$titleValue,$buttonActions = ['show', 'edit', 'delete'],$buttonOptions='')
    {

            //Value of the post Method
            $postMethod = 'POST';
            //if the application is laravel then csrf is used
            if (function_exists('csrf_token'))
            {
              $token = csrf_token();    
            }elseif (!function_exists('csrf_token')) 
            //else if the mcrypt id is used if the function exits
                {
                    if (function_exists('mcrypt_create_iv')) 
                    {
                        // if the mcrypt_create_iv id is used if the function exits the set the token
                        $token = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
                    }
                    else{
                        // elseopenssl_random_pseudo_bytes is used if the function exits the set the token
                        $token = bin2hex(openssl_random_pseudo_bytes(32));
                    }
                }        

            //action button Value 
            //(url()->full()) will pass the current browser url to the function[only aplicable in laravel]
            $urlWithId  =$fullUrl.'/'.$id;
            //Charset UsedByFrom
            $charset = 'UTF-8';

            // Start Delete Button Arguments
            //title for delete functions
            $deleteFunctionTitle = 'Delete';
            //class name for the deletebutton
            $deleteButtonClass = 'btn-delete btn btn-xs btn-danger';
            //Icon for the delete Button
            $deleteButtonIcon = 'fa fa-trash';
            //text for the delete button
            $deleteButtonText  = 'Delete Button';
            //dialog Which needs to be displayes while deleting the record
            $deleteConfirmationDialog = 'Are You Sure t';

            $deleteButtonTooltopPostion = 'top';
            // End Delete Button Arguments


             // Start Edit Button Arguments
            //title for Edit functions
            $editFunctionTitle = 'Edit';
            $editButtonClass = 'btn-delete btn btn-xs btn-primary';
            //Icon for the Edit Button
            $editButtonIcon = 'fa fa-pencil';
            //text for the Edit button
            $editButtonText  = 'Edit Button';
            $editButtonTooltopPostion = 'top';
            // End Edit Button Arguments


            // Start Show Button Arguments
            //title for Edit functions
            $showFunctionTitle = 'Show';
            $showButtonClass = 'btn-delete btn btn-xs btn-primary';
            //Icon for the Show Button
            $showButtonIcon = 'fa fa-eye';
            //text for the Show button
            $showButtonText  = 'Show Button';
            $showButtonTooltopPostion = 'top';
            // End Show Button Arguments
            //Start Arguments for DropDown Buttons
            $dropDownButtonName = 'Actions';
            //End Arguments for DropDown Buttons


            $showButton = '';
            $showButton .='
                <a href="'.$fullUrl.'/'.$id.'"class="'.$showButtonClass.'"data-toggle="tooltip"data-placement="'.$showButtonTooltopPostion.'"title="'.$showFunctionTitle.'-'.$titleValue.'">
                    <i class="'.$showButtonIcon.'"></i> '.$showButtonText.'
                </a>
            ';

            $editButton ='';
            $editButton .='
                    <a href="'.$urlWithId.'/edit'.'"class="'.$editButtonClass.'"data-toggle="tooltip"data-placement="'.$editButtonTooltopPostion.'" title="'.$editFunctionTitle.'-'.$titleValue.'">
                        <i class="'.$editButtonIcon.'"></i> '.$editButtonText.'
                    </a>
                ';


            $deleteButton='';
            $deleteButton .='
                    <form id="form-delete-row' . $id . '"  method="'.$postMethod.'" action="'.$urlWithId.'" accept-charset="'.$charset.'"style="display: inline" onSubmit="return confirm(&quot;'.$deleteConfirmationDialog.'&quot;)">
                        <input name="_method" type="hidden" value="DELETE">
                        <input name="_token" type="hidden" value="'.$token.'">
                        <input name="_id" type="hidden" value="'.$id.'">
                        <button type="submit"class="'.$deleteButtonClass.'"data-toggle="tooltip"data-placement="'.$deleteButtonTooltopPostion.'" title="'.$deleteFunctionTitle.'-'.$titleValue.'">
                            <i class="'.$deleteButtonIcon.'"></i>'.$deleteButtonText.'
                        </button>
                    </form>
                ';

            $actionButtons = '';

            foreach ($buttonActions as $buttonAction) 
            {
                if ($buttonAction == 'show') 
                {
                    $actionButtons .= $showButton;
                }
                if ($buttonAction == 'edit') 
                {
                    $actionButtons .= $editButton;
                }
                if ($buttonAction == 'delete') 
                {
                    $actionButtons .= $deleteButton;
                }                
            }
            if (empty($buttonOptions)) 
            {
                return  $actionButtons;
            }
            elseif (!empty($buttonOptions)) 
            {
                if ($buttonOptions == 'group') 
                {

                    $buttonGroup = '<div class="btn-group" role="group" aria-label="">
                    '.$actionButtons.'
                    </div>';
                    return $buttonGroup;
                }elseif($buttonOptions == 'dropdown') 
                {
                    $dropDownButton  =
                        '<div class="dropdown">
                          <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            '.$dropDownButtonName.'
                          </button>
                          <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                          '.$actionButtons.'
                          </div>
                        </div>
                        ';
                        return $dropDownButton;
                }else
                {
                    return  'only <code>group</code> and <code>dropdown</code> is Available ';
                }

            }                       
        }

有了这个

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

如果您发现按钮中有任何错误或问题,请在以下部分中进行评论,以改善我的帮助脚本

非常注意,如果您现在{!! Computer::tableActionButtons(url()->full(),$inventory->id,$inventory->firstName,['edit',delete,delete],'dropdown'); !!}

,这是拉拉尔语EXPERTS

只需在

观看本教程即可
BEGGINER DONT USE IT

希望可以节省时间