2016-10-04 115 views
2

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

我的编辑表单起作用。访问计算机/ {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 

显示表这是我的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> 

回答

0

试试这个:

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

或者你可以使用表格(Laravel集体的方式):

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

表单集体工作。该href没有,我尝试了多种方式来做到这一点。非常感谢! – Afonte

0

代替使用按钮,我会使用<a>标签。

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

url()功能是Laravel helper function

还..我敢肯定也有这样的事情足够的例子,所以一定要第一个Google提问。

+0

我以前这个尝试,但按钮不会改变,并说computer.edit,但它不打开编辑表单。 – Afonte