やりたいこと
複数のテーブルを外部キーで紐づき、一度のPOSTでDBに登録したいのですが、方法が見出せずにいますので是非アドバイスをお願いいたします。
現状としては、employeeテーブルとgoodsテーブルの情報を「社員登録する」で登録はされるのですが、これを「employee_id」で紐づきたいものです。
各テーブル
web.php
<?php Route::get('/', 'EmployeesController@index'); Auth::routes(); Route::get('index', 'HomeController@index')->name('index'); Route::group(['middleware' => 'auth'], function () { Route::resource('goods', 'GoodsController', ['only' => ['create', 'store', 'destroy']]); Route::resource('index', 'EmployeesController', ['only' => ['index']]); Route::resource('employee_create', 'EmployeesController', ['only' => ['index', 'create','store']]); });
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']; public function employee() { return $this->belongsTo(Employee::class); } }
EmployeesController
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Employee; use App\Goods; class EmployeesController extends Controller { public function index() { $employees = Employee::orderBy('id','desc')->get(); return view('index', ['employees' => $employees, ]); } public function create() { $user =\Auth::user(); return view('employee_create'); } public function store(Request $request) { $request->validate([ 'employee_id' => 'required|min:6|max:6', 'employee_name' => 'required', 'office' => 'required', ]); $employee = new Employee; $employee->employee_id = $request->input('employee_id'); $employee->employee_name = $request->input('employee_name'); $employee->office = $request->input('office'); $employee->save(); $goods = new Goods; $goods->uniform = $request->input('uniform'); $goods->winter_clothes = $request->input('winter_clothes'); $goods->shoes = $request->input('shoes'); $goods->other = $request->input('other'); $goods->memo = $request->input('memo'); $goods->employee_id = $employee->id; $goods->save(); return redirect(route('employee_create.index'))->with('flash_message','社員を登録しました'); } }
employee_create.blade.php
@extends('layouts.app') @extends('layouts.app') @section('content') <div class="container"> <div class="login-container"> <h3>新規社員登録</h3> <div class="card-body"> <form method="POST" action="{{ route('employee_create.store') }}"> @csrf <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="employee_id" class="form-control" value="{{old('employee_id')}}" 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="tel" name="employee_name" class="form-control" value="{{old('employee_name')}}" 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"> <select name="office" class="form-control"> <option value="">所属事業所を選択してください</option> <option value="1営業所">1営業所</option> <option value="2営業所">2営業所</option> <option value="3営業所">3営業所</option> <option value="4営業所">4営業所</option> <option value="5営業所">5営業所</option> </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="unifrom" class="form-control" value="{{old('unifrom')}}" 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="winter_clothes" class="form-control" value="{{old('winter_clothes')}}" 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="shoes" class="form-control" value="{{old('shoes')}}" 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="other" class="form-control" value="{{old('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="{{old('memo')}}" placeholder="メモを入力してください"> </div> </div> <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
以下、追加分です
create_employee_table.php
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateEmployeesTable extends Migration { public function up() { Schema::create('employees', function (Blueprint $table) { $table->bigIncrements('id'); $table->integer('employee_id'); $table->string('employee_name'); $table->string('office'); $table->timestamps(); }); } public function down() { Schema::dropIfExists('employees'); } }
create_goods_table.php
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateGoodsTable extends Migration { public function up() { Schema::create('goods', function (Blueprint $table) { $table->bigIncrements('id'); $table->unsignedBigInteger('employee_id'); $table->string('uniform'); $table->string('winter_clothes'); $table->string('shoes'); $table->string('other'); $table->string('memo'); $table->timestamps(); $table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade'); }); } public function down() { Schema::dropIfExists('goods'); } }
マイグレーション実行時エラー画像
回答1件
あなたの回答
tips
プレビュー