やりたいこと
メソッドの未定義エラーについて、どの箇所を修正すれば良いか分かり兼ねるためご教示いただければと思います。
レイアウトとして「app.blade.php」を全てのビューに紐付けており、他のビューではエラーが生じないのですが、editビューでは下記エラーが発生します。コントローラーのupdateアクションにメソッドの定義すべきなのか、他に何か方法があるのかアドバイスを頂ければと思います。
エラー画面
■web.php
Route::get('guest', 'Auth\LoginController@guestLogin')->name('login.guest'); Route::get('/', 'EmployeesController@index'); Route::get('/employee.search', 'EmployeesController@search')->name('employee.search'); Auth::routes(); Route::group(['middleware' => 'auth'], function () { Route::resource('index', 'EmployeesController', ['only' => ['index']]); Route::resource('employee_create', 'EmployeesController'); });
■Employee.php(モデル)
namespace App; use IlluminateSupportFacadesDB; use Illuminate\Database\Eloquent\Model; class Employee extends Model { protected $fillable = ['id','emlpoyee_id','employee_name','office','employee_image']; protected $guarded = array('id'); public function goods() { return $this->hasMany(Goods::class); } }
■EmployeesController.php(コントローラー)※一部省略
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Employee; use App\Goods; use Illuminate\Support\Facades\Storage; class EmployeesController extends Controller { public function index() { $employees = Employee::with('goods')->paginate(10); return view('index', ['employees' => $employees, ]); } public function show($id) { $employee = Employee::find($id); return view('employee_show', compact('employee')); } public function edit($id) { $employee = Employee::find($id); return view('employee_edit', compact('employee')); } public function update(Request $request, $id) { $employee = Employee::find($id); $employee->office = $request->office; $employee->employee_image = $request->employee_image; $employee->save(); $goods = $employee->goods()->latest()->first(); $goods->uniform = $request->uniform; $goods->winter_clothes = $request->winter_clothes; $goods->shoes = $request->shoes; $goods->other = $request->other; $goods->memo = $request->memo; $goods->save(); return redirect(route('employee_create.index'))->with('flash_message','社員情報を更新しました'); } public function search(Request $request) { $employee = Employee::where('employee_name', 'like', "%{$request->search}%") ->orWhere('office', 'like', "%{$request->search}%") ->paginate(3); $search_result = $request->search.'の検索結果は'.$employee->total().'件です'; return view('index', [ 'employees' => $employee, 'search_result' => $search_result, 'search_query' => $request->search ]); } }
■app.blade.php(ビュー)※一部省略
<body> @include('commons.header') <!-- search --> @isset($search_result) <h5 style="padding: 15px;">{{ $search_result }}</h5> @endisset @if(isset($employee_name)) {{ $employee->appends(['employee_name' => $employee_name])->links() }} @elseif(isset($office)) {{ $employee->appends(['office' => $office])->links() }} @endif @include('commons.footer') </body>
■employee_edit.blade.php(ビュー)※一部省略
@extends('layouts.app') @section('content') <div class="container"> <div class="login-container"> <h3>{{ $employee->employee_name }}({{ $employee->employee_id }})</h3> <div class="card-body"> <form method="POST" action="{{ route('employee_create.update', $employee->id) }}"> @csrf @method('PATCH') <div class="form-group row"> <label class="col-md-4 col-form-label text-md-right">所属</label> <div class="col-md-6"> <select name="office" class="form-control"> <option value='disabled' style='display:none'></option> @foreach(['1営業所', '2営業所', '3営業所'] as $office) <option @if ($employee->office === $office) selected @endif >{{ $office }}</option> @endforeach </select> </div> </div> @foreach($employee->goods as $goods) <div class="form-group row"> <label class="col-md-4 col-form-label text-md-right">制服</label> <div class="col-md-6"> <select name="uniform" class="form-control"> <option value='disabled' style='display:none'></option> @foreach(['S', 'M', 'L', 'XL'] as $size) <option @if ($goods->uniform === $size) selected @endif >{{ $size }}</option> @endforeach </select> </div> </div> <div class="form-group row"> <label class="col-md-4 col-form-label text-md-right">防寒着</label> <div class="col-md-6"> <select name="winter_clothes" class="form-control"> <option value='disabled' style='display:none'></option> @foreach(['S', 'M', 'L', 'XL'] as $size) <option @if ($goods->winter_clothes === $size) selected @endif >{{ $size }}</option> @endforeach </select> </div> </div> <div class="form-group row"> <label class="col-md-4 col-form-label text-md-right">靴</label> <div class="col-md-6"> <select name="shoes" class="form-control"> <option value='disabled' style='display:none'></option> @foreach(['23㎝', '23.5㎝', '24㎝', '24.5㎝', '25㎝', '25.5㎝', '26㎝', '26.5㎝', '27㎝', '27.5㎝', '28㎝', '28.5㎝'] as $size) <option @if ($goods->shoes === $size) selected @endif >{{ $size }}</option> @endforeach </select> </div> </div> <div class="form-group row"> <label class="col-md-4 col-form-label text-md-right">その他</label> <div class="col-md-6"> <input type="text" name="other" class="form-control" value="{{ $goods->other }}" placeholder="その他を入力してください"> </div> </div> <div class="form-group row"> <label class="col-md-4 col-form-label text-md-right">メモ</label> <div class="col-md-6"> <input type="text" name="memo" class="form-control" value="{{ $goods->memo }}" placeholder="メモを入力してください"> </div> </div> @endforeach <div class="form-group row mb-0"> <div class="col-md-8 offset-md-4"> <input type="submit" value="社員情報を更新する" class="btn btn-primary"> <input type="reset" value="戻る" class="btn btn-secondary" onclick='window.history.back(-1);'> </div> </div> </form> </div> </div> </div> @endsection
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/08/13 12:10