laravelを使用して、DB内テーブルレコードの対象カラムから特定の文字列だけを抜き出し、条件分岐で表示を変えたいと考えています。
手順としては
1 foreachでテーブル(icons)レコードを1つずつ展開
2 テーブルレコードのhealthカラムに含まれる数値によって、fontawesomeのアイコン表示を切り替える(例えば値が1ならリンゴのicon、2なら鳥のアイコンといったようにしたい)
しかし、iconテーブルの中から対象レコード1つだけを指定する方法がわかりません。
php
1if( Icon::where('health', 'like', '%1%')->get() )
という形でhealthカラムを取得すると当然なのですが、全てのカラムを取得してしまいます。そのためif文の条件分岐によってアイコンの表示分けをすることができないのです。
しかし $val->healthとすると、「$val->healthといったカラムは見つからないよ」と怒られてしまいます。
どのようにすれば、対象レコードのカラム1つだけを取得することができるのでしょうか。
コードは以下のものであり、コメントで「 // アイコンのカレンダー内表示」と記されているところ以下が今回お聞きしたい問題箇所となります。
●Calendar.php
php
1<?php 2namespace App; 3use App\User; 4use Illuminate\Support\Facades\Log; 5use App\Memo; 6use Illuminate\Support\Facades\Auth; 7 class Calendar 8{ 9 private $html; 10 private $memos; 11 private $icons; 12 function __construct($memos, $icons) { 13 $this->memos = $memos; 14 $this->icons = $icons; 15 } 16 public function showCalendarTag($m, $y) 17 { 18 $year = $y; 19 $month = $m; 20 if ($year == null) { 21 // システム日付を取得する。 22 $year = date("Y"); 23 $month = date("m"); 24 } 25 $firstWeekDay = date("w", mktime(0, 0, 0, $month, 1, $year)); // 1日の曜日(0:日曜日、6:土曜日) 26 $lastDay = date("t", mktime(0, 0, 0, $month, 1, $year)); // 指定した月の最終日 27 // 前月 28 $prev = strtotime('-1 month',mktime(0, 0, 0, $month, 1, $year)); 29 $prev_year = date("Y",$prev); 30 $prev_month = date("m",$prev); 31 // 翌月 32 $next = strtotime('+1 month',mktime(0, 0, 0, $month, 1, $year)); 33 $next_year = date("Y",$next); 34 $next_month = date("m",$next); 35 // 日曜日からカレンダーを表示するため前月の余った日付をループの初期値にする 36 $day = 1 - $firstWeekDay; 37 $this->html = <<< EOS 38<h2> 39<a class="btn btn-primary" href="/?year={$prev_year}&month={$prev_month}" role="button"><前月</a> 40{$year}/{$month} 41<a class="btn btn-primary" href="/?year={$next_year}&month={$next_month}" role="button">翌月></a> 42</h2> 43<table class="table table-bordered" style="table-layout:fixed;"> 44<tr> 45 <th scope="col">Sun</th> 46 <th scope="col">Mon</th> 47 <th scope="col">Tue</th> 48 <th scope="col">Wed</th> 49 <th scope="col">Thu</th> 50 <th scope="col">Fri</th> 51 <th scope="col">Sat</th> 52</tr> 53EOS; 54 // カレンダーの日付部分を生成する 55 while ($day <= $lastDay) { 56 $this->html .= "<tr>"; 57 // 各週を描画するHTMLソースを生成する 58 for ($i = 0; $i < 7; $i++) { 59 if ($day <= 0 || $day > $lastDay) { 60 // 先月・来月の日付の場合 61 $this->html .= "<td> </td>"; 62 } else { 63 $this->html .= "<td>" . $day ." " . "<br>"; 64 // メモのカレンダー内表示 65 $target = date("Y-m-d", mktime(0, 0, 0, $month, $day, $year)); 66 foreach($this->memos as $val) { 67 if($val->user_id == Auth::user()->id) { 68 if ($val->memo_day == $target) { 69 $this->html .= "<a href='/memo/$val->id'>" . e($val->memo) . "</a>"; 70 break; 71 } 72 } 73 } 74 // アイコンのカレンダー内表示 75 $target = date("Y-m-d", mktime(0, 0, 0, $month, $day, $year)); 76 foreach($this->icons as $val) { 77 if($val->user_id == Auth::user()->id) { 78 if ($val->icon_day == $target) { 79 if( Icon::where('health', 'like', '%1%')->get() ){ 80 dd(Icon::where('health', 'like', '%1%')->get() ); 81 $this->html .= "<i class='fas fa-tasks fa-lg'></i>"; 82 } 83 break; 84 } 85 } 86 } 87 $this->html .= "</td>"; 88 } 89 $day++; 90 } 91 $this->html .= "</tr>"; 92 } 93 $this->html .= "</table>"; 94 return $this->html; 95 } 96}
●2019_05_27_235441_create_icons_table
php
1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreateIconsTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('icons', function (Blueprint $table) { 17 $table->increments('id'); 18 $table->date('icon_day'); 19 $table->string('health'); 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 /** 33 * Reverse the migrations. 34 * 35 * @return void 36 */ 37 public function down() 38 { 39 Schema::dropIfExists('icons'); 40 } 41}
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/04 00:34