質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

1回答

2332閲覧

Property [gender] does not exist on this collection instance.

Mason

総合スコア14

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2021/09/11 08:40

前提・実現したいこと

現在、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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

どこで起きてるか書かれてないのでコード見た感でしかないですが、Eloquentのallメソッドはモデルテーブルの全レコードを結果として返すので、列番号をもった配列が返ってきてるのではないでしょうか。

$contacts = ContactForm::all();

なのでここはコメントアウトされてるfindで良いのでは。

投稿2021/09/11 08:46

m.ts10806

総合スコア80861

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Mason

2021/09/11 09:07

index.blade.phpでエラーが発生します。 試してみましたが、Undefined variable: idエラー出ます。 しかし、$contacts = ContactForm::all();で全部取ってこないとindex.blade.phpでeachで一覧表示できないと認識してるのですが誤っているでしょうか。 よろしくお願いします。
m.ts10806

2021/09/11 09:41

>$contacts = ContactForm::all();で全部取ってこないと そのまま渡せば良いと思いますが、リストなのにその場で $contacts->genderのような参照はできないはずなので、blade内で分岐かけられるか、SQL時点でcaseにて入れた状態でデータを取得すべき(回答の主旨) コントローラでループさせてコレクションに追加していく手もありますが、二度手間でメモリ無駄食いなので。
m.ts10806

2021/09/11 09:42

>試してみましたが、Undefined variable: idエラー出ます。 どう組んでどの行で出たエラーなのか分からないですが、エラーの通り$idが未定義なのでしょうね。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問