3

Im new in Laravel. Currently im facing a problem where I am stuck in the middle of updating details from all child but was fine in updating parent's details. So here i'll show one of the child which is Staff(Process as Parent)

1) StaffController.php (Update Method)

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

I've tried the commented code, still no changes were made when enter new details.

2) edit.blade.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

Also tried the commented form above, it gave me Array to Conversion Error.

3) index.blade.php(Edit link is in this blade file)

@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(Route file)

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

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

Any suggestion and answer are much welcomed and appreciated.

Community
  • 1
  • 1

1 Answers1

1

I've found a solution from my friend, which is the silly mistake in the edit.blade.php update form.

the (name="name") supposed to changed according to its variable name in database table

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

So, below is the correct one:

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

Thanks for now, it works and can update the details.