laravelで作成した以下の月めくりカレンダーにメモ機能を持たせたいと考えています。
##具体的に持たせたい機能
①各日付をクリックすると”その日付だけ”に紐づいたデータ(メモ)の挿入ができる
②メモの入力フォームは、日付部分をクリックするとモーダルウィンドウで表示させる。
③また日付をクリックすることでその日付を表す固有番号(例えば2019年5月1日なら2019-05-01など)を識別して、入力フォームのaction先に指定できる。
の3点です。
##相談要旨
上記3点の機能を持たせることができる操作について、ご教授いただけますと幸いです。(コードを一から書いていただくというよりは、参考となるようなサイト、もしくは助言をいただくだけで構いません。)
##コード
以下、laravelで記述したコードの中でも必要かと思われるファイルのコードを記載します。
#####2019_05_27_235037_create_memos_table(メモ用テーブルのマイグレ)
php
1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreateMemosTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('memos', function (Blueprint $table) { 17 $table->increments('id'); 18 $table->datetime('memo_day'); 19 $table->text('memo'); 20 $table->integer('user_id')->unsigned(); 21 $table->timestamps(); 22 23 $table 24 ->foreign('user_id') 25 ->references('id') 26 ->on('users') 27 ->onDelete('cascade'); 28 }); 29 } 30 31 /** 32 * Reverse the migrations. 33 * 34 * @return void 35 */ 36 public function down() 37 { 38 Schema::dropIfExists('memos'); 39 } 40} 41
#####Calendar.php(実質、html部分を司るファイル)
php
1<?php 2namespace App; 3use App\User; 4 5class Calendar 6{ 7 private $html; 8 private $holidays; 9 10 function __construct($holidays) { 11 $this->$holidays = $holidays; 12 } 13 14 public function showCalendarTag($m, $y) 15 { 16 $year = $y; 17 $month = $m; 18 if ($year == null) { 19 // システム日付を取得する。 20 $year = date("Y"); 21 $month = date("m"); 22 } 23 $firstWeekDay = date("w", mktime(0, 0, 0, $month, 1, $year)); // 1日の曜日(0:日曜日、6:土曜日) 24 $lastDay = date("t", mktime(0, 0, 0, $month, 1, $year)); // 指定した月の最終日 25 // 前月 26 $prev = strtotime('-1 month',mktime(0, 0, 0, $month, 1, $year)); 27 $prev_year = date("Y",$prev); 28 $prev_month = date("m",$prev); 29 // 翌月 30 $next = strtotime('+1 month',mktime(0, 0, 0, $month, 1, $year)); 31 $next_year = date("Y",$next); 32 $next_month = date("m",$next); 33 // 日曜日からカレンダーを表示するため前月の余った日付をループの初期値にする 34 $day = 1 - $firstWeekDay; 35 $this->html = <<< EOS 36<h2> 37<a class="btn btn-primary" href="/?year={$prev_year}&month={$prev_month}" role="button"><前月</a> 38{$year}年{$month}月 39<a class="btn btn-primary" href="/?year={$next_year}&month={$next_month}" role="button">翌月></a> 40</h2> 41<table class="table table-bordered" style="table-layout:fixed;"> 42<tr> 43 <th scope="col">日</th> 44 <th scope="col">月</th> 45 <th scope="col">火</th> 46 <th scope="col">水</th> 47 <th scope="col">木</th> 48 <th scope="col">金</th> 49 <th scope="col">土</th> 50</tr> 51EOS; 52 // カレンダーの日付部分を生成する 53 while ($day <= $lastDay) { 54 $this->html .= "<tr>"; 55 // 各週を描画するHTMLソースを生成する 56 for ($i = 0; $i < 7; $i++) { 57 if ($day <= 0 || $day > $lastDay) { 58 // 先月・来月の日付の場合 59 $this->html .= "<td> </td>"; 60 } else { 61 $this->html .= "<td>" . $day ." " . "<br>"; 62 $target = date("Y-m-d", mktime(0, 0, 0, $month, $day, $year)); 63 foreach((array)$this->holidays as $val) { 64 if ($val->day == $target) { 65 if($val->user_id == auth()->id()) 66 { 67 $this->html .= e($val->description); 68 } 69 break; 70 } 71 } 72 $this->html .= "</td>"; 73 } 74 $day++; 75 } 76 77 $this->html .= "</tr>"; 78 } 79 $this->html .= '</table>'; 80 return $this->html; 81 } 82}