Forms 将编辑按钮从表格链接到Laravel编辑表单

Forms 将编辑按钮从表格链接到Laravel编辑表单,forms,laravel,laravel-5,laravel-5.2,Forms,Laravel,Laravel 5,Laravel 5.2,我有一个表,显示数据库中的数据。在每行的末尾,我都有一个编辑/更新按钮。我希望它在点击编辑按钮时引用编辑表单 我的编辑表格有效。我可以在访问计算机时访问数据/{id}/edit,表单显示当前数据,我可以编辑数据,提交更新,并在数据库(mysql)中更新 这是我的index.blade.php,它显示带有更新按钮的表 @extends('layout') @section('content') <h1>Inventory</h1> <ta

我有一个表,显示数据库中的数据。在每行的末尾,我都有一个编辑/更新按钮。我希望它在点击编辑按钮时引用编辑表单

我的编辑表格有效。我可以在访问计算机时访问数据/{id}/edit,表单显示当前数据,我可以编辑数据,提交更新,并在数据库(mysql)中更新

这是我的index.blade.php,它显示带有更新按钮的表

@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
@extends('layout'))
@节(“内容”)
库存
姓
名字
部门
建筑
房间
制造商
装置
模型
服务标签
Mac地址
地位
评论
@foreach($存货作为$存货)
{{$inventory->lastName}
{{$inventory->firstName}
{{$inventory->department}
{{$inventory->building}
{{$inventory->room}
{{$inventory->manufacturer}
{{$inventory->device}
{{$inventory->model}
{{$inventory->tag}
{{$inventory->macAddress}
{{$inventory->status}
{{$inventory->comments}
{{--需要按钮打开我的编辑表单--}
{{trans('computers.edit')}
{{----}}
@endforeach
@停止
这是我的form.blade.php——这是我在create.blade.php和edit.blade.php中包含的一部分,这两个页面都可以使用

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

{!!Form::label('lastName','lastName:')
{!!Form::text('lastName',null,['class'=>'Form control'])
{!!Form::label('firstName','firstName:')
{!!Form::text('firstName',null,['class'=>'Form control'])
{!!表单::标签('部门','部门:')
{!!Form::text('department',null,['class'=>'Form control'])
{!!Form::label('building','building:')
{!!Form::select('building',['vanHall'=>'Vanderbilt Hal',
'wilf'=>'wilf Hall',
“dag”=>“D Agostino Hall”,
“弗曼霍尔”=>“弗曼大厅”,
“wsn”=>“wsn”,
“美世”=>“美世”,
“培训”=>“培训室”,
'存储'=>'存储'
{!!表单::标签('room','room:')
{!!Form::text('room',null,['class'=>'Form control'])
{!!Form::label('制造商','制造商:')
{!!Form::text('manufacturer',null,['class'=>'Form control'])
{!!Form::label('device','device:')
{!!表单::选择('device',['desktop'=>'desktop',
“笔记本电脑”=>“笔记本电脑”,
“教室”=>“教室”,
“打印机”=>“打印机”,
“mifi”=>“mifi”,
“panopto”=>“panopto”,
“其他”=>“其他”,
],null,['placeholder'=>'选择设备'])
{!!Form::label('model','model:')
{!!Form::text('model',null,['class'=>'Form control'])
{!!Form::label('tag','Service tag:')
{!!Form::text('tag',null,['class'=>'Form control'])
{!!Form::label('macAddress','macAddress:')
{!!Form::text('macAddress',null,['class'=>'Form control'])
{!!Form::label('status','status:')
{!!表单::选择('status',['active'=>'active',
“非活动”=>“非活动”,
],null,['placeholder'=>'Status'])
{!!表单::标签('comments','comments:')
{!!Form::text('comments',null,['class'=>'Form control'])

{!!表单::提交($submitButtonText,['class'=>'btn btn主表单控件']) {{--提交--}
我将使用

url()
函数是一个

另外,我相信这样的例子已经足够多了,所以一定要先用谷歌搜索你的问题。

试试这个:

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

很抱歉迟了答复

如果你正在创建一个更大的
项目
,如果你不喜欢成为
的人,请在每个

index.balde.php
文件,用于生成编辑、显示、删除按钮

只需使用我的助手函数

例如:

拉威尔5.7 打开您的模型,它可能是
计算机
某些模型

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

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 ';
                }

            }                       
        }
现在所有的都设置好了,然后打开index.blade.php

并替换您的代码

<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>
如果您在按钮中发现任何错误或问题,请在下面的部分中进行评论,以改进我的助手脚本

非常小心地注意,这是为LARAVEL
专家提供的
如果您现在是乞丐,请不要使用它

只需在

https://appdividend.com/2018/09/06/laravel-5-7-crud-example-tutorial/

https://laracasts.com/series/laravel-from-scratch-2018

希望它能节省时间

我以前试过,但按钮确实变了,显示为computer.edit,但它不会打开编辑表单。表单集合工作正常。href没有,我尝试了多种方法来完成此操作。非常感谢!
<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'); !!}
https://appdividend.com/2018/09/06/laravel-5-7-crud-example-tutorial/

https://laracasts.com/series/laravel-from-scratch-2018