前提・実現したいこと
Laravelを使って、特定品番の部品を一覧で表示するアプリケーションを作っています。
view側のロジックを減らそうと思ってModelの中で整形していたのですが、
DBのテーブル名、項目名にも、構造にもがっつり依存したものが出来上がってしまいました。
機能を切り出そうにも、どこから手をつけていいものかわかりません。
アドバイス頂けないでしょうか。
更に追加したい機能
- itemsテーブルに品番が存在しなかったときのリダイレクト
- 画像ファイルの存在チェック
- 部品としては登録されているけど画像が登録されていない/所定の場所にないときにnoimage.jpgに変換
該当のソースコード
itemsテーブル//製品品番が入っているテーブル
id(品番) | name(品名) | planID(図面ID) |
---|---|---|
IPX-SG | アイフォンエックス//スペースグレー | IP10 |
IPX-S | アイフォンエックス//シルバー | IP10 |
IP8-R | アイフォンエイト//レッド | IP8 |
items_partsテーブル//製品ごとの部品の品番が入っているテーブル
id | A(A項目の部品) | B | C |
---|---|---|---|
IPX-SG | IP-101 | IP-201【2009年5月より前の製造分】,IP-202【2009年5月以降の製造分】 | |
IPX-S | IP-102 | IP-201【2009年5月より前の製造分】,IP-202【2009年5月以降の製造分】 | |
IP8-R | IP-105 | IP-201 | IP-301 |
plans_columnsテーブル//図面ごとの項目名が入っているテーブル
planID | A(項目名) | B | C |
---|---|---|---|
IP10 | カバー | 液晶 | |
IP8 | カバー | 液晶 | イヤホンジャック |
parts_specificationテーブル//部品品番ごとの明細や画像ファイル名が入っているテーブル
part_id | part_specifications | part_imageFile |
---|---|---|
IP-101 | カバー1枚、カメラ、スピーカー | IP-101.jpg |
IP-105 | カバー1枚、カメラ | IP-105.jpg |
IP-201 | 液晶 | IP-201.jpg |
IP-202 | 液晶(有機EL) | IP-202.jpg |
IP-301 | イヤホンジャック | noimage.jpg |
php
1public function tableSwitch($newTable, $primaryKey = 'id', $keyType = 'string'){ 2 $this->table = $newTable; 3 $this->setKeyName($primaryKey); 4 $this->setKeyType($keyType); //実質意味なし 5 $this->incrementing = false; //実質意味なし 6 } 7 8public function getOverallItemInfomation($get) 9 { 10 $this->tableSwitch('items','id'); 11 $item = $this 12 ->select('id', 'name', 'planID') 13 ->where('id', $get)->first()->toArray(); 14 15 // 部品情報 16 $this->tableSwitch('items_parts','id'); 17 $parts = $this->where('id', $get)->first()->toArray(); 18 19 // 項目名 20 $this->tableSwitch('plans_columns','planID'); 21 $plan_columns = $this->where('planID', $item['planID'])->first()->toArray(); 22 23 // 部品分割 24 foreach ( $parts as $key => $part ){ 25 $tmp = preg_replace("/[(|【].+?[)|】]/u", "", $part); //品番に【】が含まれていたらそれを取り除く 26 $degradedPartsWithComment[$key] = explode(',', $tmp); 27 } 28 foreach ( $parts as $key => $part ){ 29 $degradedPartsNOComment[$key] = explode(',', $part); 30 } 31 32 $this->tableSwitch('parts_specification','part_id'); 33 foreach ( $degradedPartsWithComment as $key => $column ){ 34 foreach ($column as $part ) { 35 $tmp = $this->select('part_specifications', 'part_imageFile')->where('part_id', $part)->first(); 36 if ( !empty($tmp) ){ 37 $specifications[$key][] = $tmp->toArray(); 38 } else { 39 $specifications[$key] = null; 40 } 41 } 42 } 43 44 unset($parts['id']); 45 unset($parts['note']); 46 47 foreach ( $specifications as $key => $val ) { 48 if ( !is_array($val) ) $val = array(); 49 $spec[$key] = array_pluck($val, "part_specifications"); 50 $imageFile[$key] = array_pluck($val, "part_imageFile"); 51 } 52 53 foreach ( $parts as $key => $part ){ 54 if ( !empty($part) ){ 55 $test[$key]['column'] = $plan_columns[$key]; 56 $test[$key]['part'] = $degradedPartsNOComment[$key]; 57 $test[$key]['spec'] = $spec[$key]; 58 $test[$key]['imageFile'] = $imageFile[$key]; 59 } 60 } 61 62 $item['specs'] = $test; 63 64 return $test; 65 }
html
1// print_r($item)の結果であり、最終目標である形 2Array 3( 4 [id] => IPX-SG 5 [name] => アイフォンエックス//スペースグレー 6 [planID] => IP10 7 [specs] => Array 8 ( 9 [A] => Array 10 ( 11 [column] => カバー 12 [part] => Array 13 ( 14 [0] => IP-101 15 ) 16 17 [spec] => Array 18 ( 19 [0] => カバー1枚、カメラ、スピーカー 20 ) 21 22 [imageFile] => Array 23 ( 24 [0] => IP-101.jpg 25 ) 26 27 ) 28 29 [B] => Array 30 ( 31 [column] => 液晶 32 [part] => Array 33 ( 34 [0] => IP-201【2009年5月より前の製造分】 35 [1] => IP-202【2009年5月以降の製造分】 36 ) 37 38 [spec] => Array 39 ( 40 [0] => 液晶 41 [1] => 液晶(有機EL) 42 ) 43 44 [imageFile] => Array 45 ( 46 [0] => IP-201.jpg 47 [1] => IP-202.jpg 48 ) 49 50 ) 51} 52 53<section class="plan__list"> 54 {% for %} //部品の数だけ繰り返す 55 <div class="plan__list__part"> 56 <h3 class="plan__list__title">{{ 項目名 }}</h3> 57 <div class="plan__list__content"> 58 {# 左側 #} 59 <div class="plan__list__content--left"> 60 {% for %} 61 <img src="{{ 部品の画像 }}"> 62 {% endfor %} 63 </div> 64 {# 右側 #} 65 <div class="plan__list__content--right"> 66 {# 品番 #} 67 <div class="plan__list__content--right__column"> 68 <div>品番</div> 69 <div> 70 {% for %} 71 {{ 部品の品番 }} 72 {% endfor %} 73 </div> 74 </div> 75 <hr class="plan__list__content__hr"> 76 {# 明細 #} 77 <div class="plan__list__content--right__column"> 78 <div>明細</div> 79 <div> 80 {% for %} 81 {{ 部品の明細 }} 82 {% endfor %} 83 </div> 84 </div> 85 </div> 86 </div> 87 </div> 88 {% endfor %} 89</section> 90
補足情報(FW/ツールのバージョンなど)
PHP 7.2.3
Laravel 5.6.3(現在移行作業中)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。