##分からないこと
ユーザーごとに管理できる書き込み可能なカレンダーアプリを製作しています。
しかし、外部キー制約で設定したuserテーブル(親テーブル)がholidays(子テーブル)のカラムに保存させることができません。
##実現させたいこと
休日の日付と予定の内容を登録すると同時にログインしているユーザーのidをカラムに保存し、カレンダーに書き込んだユーザー以外は予定を見られないようにしたいです。
##実際のコード
◉migrationファイル
2014_10_12_000000_create_users_table
php
1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreateUsersTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('users', function (Blueprint $table) { 17 $table->increments('id'); 18 $table->string('name'); 19 $table->string('email')->unique(); 20 $table->timestamp('email_verified_at')->nullable(); 21 $table->string('password'); 22 $table->rememberToken(); 23 $table->timestamps(); 24 }); 25 } 26 27 /** 28 * Reverse the migrations. 29 * 30 * @return void 31 */ 32 public function down() 33 { 34 Schema::dropIfExists('holidays'); 35 Schema::dropIfExists('users'); 36 } 37}
2019_05_08_084435_holiday
php
1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class Holiday extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('holidays', function (Blueprint $table) { 17 $table->increments('id'); 18 $table->date('day'); 19 $table->string('description'); 20 $table->integer('user_id')->unsigned(); 21 $table->timestamps(); 22 23 // 参照制約(Usersテーブルへの外部キー) 24 $table 25 ->foreign('user_id') 26 ->references('id') 27 ->on('users') 28 ->onDelete('cascade'); 29 }); 30 } 31 32 /** 33 * Reverse the migrations. 34 * 35 * @return void 36 */ 37 public function down() 38 { 39 Schema::dropIfExists('holidays'); 40 Schema::dropIfExists('holidays'); 41 } 42} 43
◉Controller
CalendarController
php
1<?php 2namespace App\Http\Controllers; 3use App\Holiday; 4use App\Calendar; 5use App\User; 6use Illuminate\Http\Request; 7class CalendarController extends Controller 8{ 9 // 10 public function __construct() 11 { 12 $this->middleware('auth'); 13 } 14 15 public function index(Request $request) 16 { 17 18 $list = Holiday::all(); 19 $cal = new Calendar($list); 20 $tag = $cal->showCalendarTag($request->month,$request->year); 21 return view('calendar.index', ['cal_tag' => $tag]); 22 } 23 public function getHoliday(Request $request) 24 { 25 // 休日データ取得 26 $data = new Holiday(); 27 $list = Holiday::all(); 28 return view('calendar.holiday', ['list' => $list,'data' => $data]); 29 } 30 public function getHolidayId($id) 31 { 32 // 休日データ取得 33 $data = new Holiday(); 34 if (isset($id)) { 35 $data = Holiday::where('id', '=', $id)->first(); 36 } 37 $list = Holiday::all(); 38 return view('calendar.holiday', ['list' => $list,'data' => $data]); 39 } 40 41 public function getUserId($id) 42 { 43 $data = new User(); 44 if (isset($id)) { 45 $userData = User::where('id', '=', $id)->first(); 46 } 47 $user = User::all(); 48 return view('calendar.holiday', ['user' => $user,'data' => $userData]); 49 } 50 51 public function deleteHoliday(Request $request) 52 { 53 // DELETEで受信した休日データの削除 54 if (isset($request->id)) { 55 $holiday = Holiday::where('id', '=', $request->id)->first(); 56 $holiday->delete(); 57 } 58 59 // 休日データ取得 60 $data = new Holiday(); 61 $list = Holiday::all(); 62 return view('calendar.holiday', ['list' => $list, 'data' => $data]); 63 } 64 public function postHoliday(Request $request) 65 { 66 $validatedData = $request->validate([ 67 'day' => 'required|date_format:Y-m-d', 68 'description' => 'required', 69 ]); 70 // POSTで受信した休日データの登録 71 $holiday = Holiday::where('user_id', '=', auth()->id())->first(); 72 if (!isset($data)) { 73 $holiday = new Holiday(); 74 } 75 if (isset($request->id)) { 76 $holiday = Holiday::where('id', '=', $request->id)->first(); 77 $holiday->day = $request->day; 78 $holiday->description = $request->description; 79 $holiday->user_id = auth()->id(); 80 $holiday->save(); 81 } else { 82 $holiday = new Holiday(); 83 $holiday->day = $request->day; 84 $holiday->description = $request->description; 85 $holiday->save(); 86 } 87 // 休日データ取得 88 $data = new Holiday(); 89 $list = Holiday::all(); 90 return view('calendar.holiday', ['list' => $list, 'data' => $data]); 91 } 92}
◉routes
web.php
php
1Route::get('/holiday','CalendarController@getHoliday'); 2Route::post('/holiday','CalendarController@postHoliday'); 3Route::get('/','CalendarController@index'); 4Route::get('/holiday/{id}','CalendarController@getHolidayId'); 5Route::delete('/holiday','CalendarController@deleteHoliday'); 6Auth::routes(); 7 8Route::get('/home', 'CalendarController@index')->name('home'); 9
◉views
holiday.blade.php
php
1@extends('layouts.app') 2@section('title', '休日設定') 3@section('content') 4 <h1>休日</h1> 5<a href="{{ url('/') }}">カレンダーに戻る</a> 6 <form method="POST" action="/holiday"> 7 <div class="form-group"> 8 {{csrf_field()}} <label for="day">日付[YYYY/MM/DD] </label><input type="text" name="day" class="form-control" id="day" value="{{$data->day}}"> 9 <label for="description">説明</label><input type="text" name="description" class="form-control" id="description" value="{{$data->description}}"> 10 </div> 11 <button type="submit" class="btn btn-primary">登録</button> 12 <input type="hidden" name="id" value="{{$data->id}}"> 13 </form> 14@if ($errors->any()) 15 <div class="alert alert-danger"> 16 <ul> 17 @foreach ($errors->all() as $error) 18 <li>{{ $error }}</li> 19 @endforeach 20 </ul> 21 </div> 22@endif 23 <table class="table"> 24 <thead> 25 <tr> 26 <th scope="col">日付</th> 27 <th scope="col">説明</th> 28 <th scope="col">作成日</th> 29 <th scope="col">更新日</th> 30 <th scope="col">削除</th> 31 </tr> 32 </thead> 33 <tbody> 34 @foreach($list as $val) 35 <tr> 36 <th scope="row"><a href="{{ url('/holiday/'.$val->id) }}">{{$val->day}}</a></th> 37 <td>{{$val->description}}</td> 38 <td>{{$val->created_at}}</td> 39 <td>{{$val->updated_at}}</td> 40 <td><form action="/holiday" method="post"> 41{{csrf_field()}} 42 <input type="hidden" name="id" value="{{$val->id}}"> 43 {{ method_field('delete') }} 44 <button class="btn btn-default" type="submit">Delete</button> 45 </form></td> 46 </tr> 47 @endforeach 48 </tbody> 49 </table> 50 51<script> 52 $( function() { 53 $( "#day" ).datepicker( 54{dateFormat: 'yy-mm-dd'}); 55 } ); 56</script> 57@endsection
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。