やりたいこと
既存DB情報の更新処理を行う際に、見出しのエラーが発生し解決できずにいますので何かアドバイスをお願いいたします。
内容としては2つのテーブル(employeesテーブル、goodsテーブル)を一度に更新するものです。goodテーブルの情報が渡っていない(null?)が故のエラーなのでしょうか?
調べても見出せずにいますので何卒宜しくお願いいたします。
__Laravel バージョン 6.20.8
__
EmployeesController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Employee; use App\Goods; class EmployeesController extends Controller public function update(Request $request, $id) { $employee = Employee::find($id); $employee->office = $request->office; $employee->save(); $goods = Goods::find($id); $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 view('employee_show', compact('employee')); }
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(['群馬営業所, '長野営業所'] 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
Employee.php
<?php namespace App; use IlluminateSupportFacadesDB; use Illuminate\Database\Eloquent\Model; class Employee extends Model { protected $fillable = ['id','emlpoyee_id','employee_name','office']; public function goods() { return $this->hasMany(Goods::class); } }
Goods.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Goods extends Model { protected $fillable = ['employee_id','uniform','winter_clothes','shoes','other','memo']; protected $guarded = array('id'); public function getDate() { return $this->belongsTo(Employee::class); } public function employee() { return $this->belongsTo(Employee::class); } }
解決した詳細
EmployeesController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Employee; use App\Goods; class EmployeesController extends Controller public function update(Request $request, $id) { $employee = Employee::find($id); $employee->office = $request->office; $employee->save(); $goods = $employee->goods()->latest()->first();//(修正後) //(修正前)$goods = Goods::find($id); $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 view('employee_show', compact('employee')); }
回答1件
あなたの回答
tips
プレビュー