前提・実現したいこと
現在、Laravelで簡単なCRUDアプリ作成しています。
indexページでgenderを表示させたいです。
発生している問題・エラーメッセージ
Property [gender] does not exist on this collection instance.
該当のソースコード
ContactFormController.php
php
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6 7// 呼び出し 8use App\Models\ContactForm; 9 10class ContactFormController extends Controller 11{ 12 /** 13 * Display a listing of the resource. 14 * 15 * @return \Illuminate\Http\Response 16 */ 17 public function index() 18 { 19 $contacts = ContactForm::all(); 20 // $contact = ContactForm::find($id); 21 // dd($contacts); 22 if($contacts->gender === 0){ 23 $gender = '男性'; 24 } 25 if($contacts->gender === 1){ 26 $gender = '女性'; 27 } 28 return view('contact.index',compact('contacts','gender')); 29 } 30 31 /** 32 * Show the form for creating a new resource. 33 * 34 * @return \Illuminate\Http\Response 35 */ 36 public function create() 37 { 38 // 39 return view('contact.create'); 40 } 41 42 /** 43 * Store a newly created resource in storage. 44 * 45 * @param \Illuminate\Http\Request $request 46 * @return \Illuminate\Http\Response 47 */ 48 public function store(Request $request) 49 { 50 $contact = new ContactForm; 51 // dd($contact); 52 53 $contact->your_name = $request->input('your_name'); 54 $contact->title = $request->input('title'); 55 $contact->email = $request->input('email'); 56 $contact->url = $request->input('url'); 57 $contact->gender = $request->input('gender'); 58 $contact->age = $request->input('age'); 59 $contact->contact = $request->input('contact'); 60 61 $contact->save(); 62 63 return redirect('contact'); 64 } 65 66 /** 67 * Display the specified resource. 68 * 69 * @param int $id 70 * @return \Illuminate\Http\Response 71 */ 72 public function show($id) 73 { 74 $contact = ContactForm::find($id); 75 if($contact->gender === 0){ 76 $gender = '男性'; 77 } 78 if($contact->gender === 1){ 79 $gender = '女性'; 80 } 81 82 if($contact->age === 1){ 83 $age = '~19歳'; 84 } 85 if($contact->age === 2){ 86 $age = '20'; 87 } 88 if($contact->age === 3){ 89 $age = '30'; 90 } 91 if($contact->age === 4){ 92 $age = '40歳'; 93 } 94 if($contact->age === 5){ 95 $age = '50'; 96 } 97 if($contact->age === 6){ 98 $age = '60歳~'; 99 } 100 return view('contact.show', compact('contact','gender','age')); 101 } 102 103 /** 104 * Show the form for editing the specified resource. 105 * 106 * @param int $id 107 * @return \Illuminate\Http\Response 108 */ 109 public function edit($id) 110 { 111 $contact = ContactForm::find($id); 112 return view('contact.edit', compact('contact')); 113 114 } 115 116 /** 117 * Update the specified resource in storage. 118 * 119 * @param \Illuminate\Http\Request $request 120 * @param int $id 121 * @return \Illuminate\Http\Response 122 */ 123 public function update(Request $request, $id) 124 { 125 $contact = ContactForm::find($id); 126 127 $contact->your_name = $request->input('your_name'); 128 $contact->title = $request->input('title'); 129 $contact->email = $request->input('email'); 130 $contact->url = $request->input('url'); 131 $contact->gender = $request->input('gender'); 132 $contact->age = $request->input('age'); 133 $contact->contact = $request->input('contact'); 134 135 $contact->save(); 136 137 return redirect('/contact'); 138 } 139 140 /** 141 * Remove the specified resource from storage. 142 * 143 * @param int $id 144 * @return \Illuminate\Http\Response 145 */ 146 public function destroy($id) 147 { 148 $contact = ContactForm::find($id); 149 $contact->delete(); 150 return redirect('/contact'); 151 // dd($contact); 152 } 153} 154
index.blade.php
php
1<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> 2 3<h1>indexです</h1> 4<a href="{{ route('contact.create') }}" class="btn btn-primary">新規登録</a> 5 6 7<table class="table"> 8 <thead class="thead-dark"> 9 <tr> 10 <th scope="col">name</th> 11 <th scope="col">title</th> 12 <th scope="col">email</th> 13 <th scope="col">urll</th> 14 <th scope="col">gender</th> 15 <th scope="col">age</th> 16 <th scope="col">contact</th> 17 </tr> 18 </thead> 19@foreach ($contacts as $contact) 20 <tbody> 21 <tr> 22 <th scope="row"><a href="{{ route('contact.show',$contact->id) }}">{{ $contact->your_name }}</a></th> 23 <td>{{ $contact->title }}</td> 24 <td>{{ $contact->email }}</td> 25 <td>{{ $contact->url }}</td> 26 <td>{{ $gender}}</td> 27 <td>{{ $contact->contact }}</td> 28 </tr> 29@endforeach 30 </tbody> 31</table>
show.blade.php
php
1<h1>詳細</h1> 2<a href="{{ route('contact.index') }}">一覧</a> 3<a href="{{ route('contact.edit', $contact->id)}}">edit</a> 4<form action="{{ route('contact.destroy', $contact->id )}}" method="POST"> 5@csrf 6@method('delete') 7<input type="submit" value="削除"> 8</form> 9 10 11<tr> 12 <th scope="row"><a href="{{ route('contact.show',$contact->id) }}">{{ $contact->your_name }}</a></th> 13 <td>{{ $contact->title }}</td> 14 <td>{{ $contact->email }}</td> 15 <td>{{ $contact->url }}</td> 16 <td>{{ $gender }}</td> 17 <td>{{ $age }}</td> 18 <td>{{ $contact->contact }}</td> 19 </tr>
試したこと
showページでは表示できることを確認。
ご教授して頂けると幸いです。
補足情報(FW/ツールのバージョンなど)
Laravel6
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/09/11 09:07
2021/09/11 09:41
2021/09/11 09:42