簡體   English   中英

Laravel 6.x - 無法在 Controller 方法(嵌套子)中更新表單

[英]Laravel 6.x - Cannot update form in Controller method(Nested-Child)

我是 Laravel 的新人。目前我面臨一個問題,我被困在更新所有孩子的詳細信息的中間,但在更新父母的詳細信息方面很好。 所以在這里我將展示一個孩子,它是員工(作為父母的過程)

1)StaffController.php(更新方法)

public function update($processes_id, Request $request, Staff $staff)
{
    // $staff->update($request->all());
    // return redirect()->route('processes.staffs.index', $processes_id);

    $request->validate([
        'staffName' => 'required',
        'staffDept' => 'required',
        'staffSection' => 'required',
        ]);

    $staff->update($request->all());
    // $staff = Process::find($id);

    // $staff->staffName = $request->input('staffName');
    // $staff->staffDept = $request->input('staffDept');
    // $staff->staffSection = $request->input('staffSection');
    // $process->save();

    return redirect()->route('processes.staffs.index')
                     ->with('success','Asset updated successfully.');
}

我已經嘗試了注釋代碼,輸入新詳細信息時仍然沒有進行任何更改

2) 編輯刀片.php

@extends('layouts.app')

@section('content')
    <h1>Edit Staff</h1>

    <!-- FORM -->
    {{-- {!! Form::open(['action' => ['ProcessesController@update', [$staff->id, $staff->processes_id]], 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
        <div class="form-group">
            {{Form::label('staffName', 'staffName:')}}
            {{Form::text('staffName', $staff->staffName, ['class' => 'form-control'])}}
        </div>

        <div class="form-group">
            {{Form::label('staffDept', 'staffDept:')}}
            {{Form::text('staffDept', $staff->staffDept, ['class' => 'form-control'])}}
        </div>

        <div class="form-group">
            {{Form::label('staffSection', 'staffSection:')}}
            {{Form::text('staffSection', $staff->staffSection, ['class' => 'form-control'])}}
        </div>

        {{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
        <a href="/processes" class="btn btn-primary">Back</a>
    </div>

    {{Form::hidden('_method', 'PUT')}}

    {!! Form::close() !!} --}}

    <form action="{{ route('processes.staffs.update', [$staff->processes_id, $staff->id]) }}" method="POST">
        @csrf
        @method('PUT')
   
         <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Staff Name:</strong>
                    <input type="text" name="name" value="{{ $staff->staffName }}" class="form-control" placeholder="Name">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Staff Department:</strong>
                    <input type="text" name="name" value="{{ $staff->staffDept }}" class="form-control" placeholder="Name">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Staff Section:</strong>
                    <input type="text" name="name" value="{{ $staff->staffSection }}" class="form-control" placeholder="Name">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12 text-left">
              <button type="submit" class="btn btn-primary">Submit</button>
              <a href="/processes" class="btn btn-primary">Back</a>
            </div>
        </div>
    </form>
@endsection

還嘗試了上面的評論形式,它給了我Array to Conversion Error

3) index.blade.php(編輯鏈接在此刀片文件中)

@extends('layouts.app')

@section('content')
    <h1>Staffs</h1>
    @if(count($staffs) > 0)
        <table class="table table-bordered">
            <tr>
                <th>No.</th>
                <th>Name</th>
                <th>Department</th>
                <th>Section</th>
                <th width="280px">Action</th>
            </tr>

            @php $i = 0 @endphp
            
            @foreach ($staffs as $staff)
            <tr>
                <td>{{ ++$i }}</td>
                <td>{{ $staff->staffName }}</td>
                <td>{{ $staff->staffDept }}</td>
                <td>{{ $staff->staffSection }}</td>
                
                <td>
                    <a class="btn btn-info" href="{{ route('processes.staffs.show',[$staff->processes_id, $staff->id]) }}">Show</a>
                    <a class="btn btn-primary" href="{{ route('processes.staffs.edit',[$staff->processes_id, $staff->id]) }}">Edit</a>
       
                    @csrf
                    {{-- @method('DELETE')
          
                    <button type="submit" class="btn btn-danger">Delete</button> --}}
                    <form action="{{ route('processes.staffs.destroy', [$staff->processes_id, $staff->id]) }}" method="POST">
                           <input type="hidden" name="_method" value="DELETE">
                           <input type="hidden" name="_token" value="{{ csrf_token() }}">
                           <input type="submit" class="btn btn-xs btn-danger" value="Delete">
                    </form>
                </td>
            </tr>
            @endforeach
        </table>

        {{-- {{$staffs->links()}} --}}
    @else
        <p>No staffs found!</p>
    @endif
    <a href="{{ route("processes.staffs.create", $processes_id) }}" class="btn btn-primary">Add New Staff</a>
    <a href="/processes" class="btn btn-primary">Back</a>
@endsection

4) web.php(路由文件)

//Asset
Route::resource('processes','ProcessesController');

// Staff
Route::resource('processes.staffs','StaffController');
Route::PUT('/processes/{process}/staffs/{staff}','StaffController@update');

非常歡迎和贊賞任何建議和答案。

我從我的朋友那里找到了解決方案,這是edit.blade.php更新表單中的愚蠢錯誤。

(name="name") 應該根據其在數據庫表中的變量名稱進行更改

<input type="text" name="name" value="{{ $staff->staffName }}" class="form-control" placeholder="Name">

所以,下面是正確的:

<input type="text" name="staffName" value="{{ $staff->staffName }}" class="form-control" placeholder="Name">

暫時感謝,它可以工作並且可以更新詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM