PHPフレームワークLaravel入門にてPHP Laravelの勉強をしております者です。
『関連レコードの有無』部分にて躓いており、どなたかエラー内容がわかる方がいらっしゃいましたら教えて頂ければと思います。恐れいりますがよろしくお願い致します。
###やりたいこと
DBからboard記入者と記入していない人の値を取得して表示させたい
###エラー内容
Laravel\laravelapp\resources\views\person\index.blade.php
Undefined variable: hasItems
###困っていること
hasメソッドでPerson側の設定を受け渡しを行い、$hasItemsの値を定義したはずなのだが、Person/index.blade.php側では値が取得できていない
###工夫したこと
10. 【PersonController.php】のpubulic funciton index()の()内に$hasItems,$noItems,$param等の変数を記入 → ×
0. return view('person.index',['param'=>$param]) →×
0. 【PersonController.php】側にuse App\Board;を追記 → ×
###OS等の環境
OS環境:Windows10
PHP環境:PHP7.3
使用しているDB:SQLite
###記載したコード一覧
【Person.php】
php
1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6use Illuminate\Database\Eloquent\Builder; 7 8class person extends Model 9{ 10 public function getData() 11 { 12 return $this->id. ':' .$this->name . ' (' . $this->age . ')'; 13 } 14 15 protected $guarded = array('id'); 16 17 public static $rules = array( 18 'name' => 'required', 19 'mail' => 'email', 20 'age' => 'integer|min:0|max:150' 21 ); 22 23 public function boards() 24 { 25 return $this->hasMany('App\Board'); 26 } 27}
【PersonController.php】
php
1<?php 2namespace App\Http\Controllers; 3use App\Person; 4use Illuminate\Http\Request; 5 6class PersonController extends Controller 7{ 8 public function index(Request $request) 9 { 10 $hasItems = Person::has('boards')->get(); 11 $noItems = Person::doesntHave('boards')->get(); 12 $param = ['hasItems' => $hasItems, 'noItems' => $noItems]; 13 return view('person.index',$param); 14 } 15 16 public function find(Request $request) 17 { 18 return view('person.find',['input'=>'']); 19 } 20 21 public function search(Request $request) 22 { 23 $min = $request->input * 1; 24 $max = $min + 10; 25 $item = Person::ageGreaterThan($min)->ageLessThan($max)->first(); 26 $param = ['input'=>$request->input, 'item'=>$item]; 27 return view('person.find',$param); 28 } 29 public function add(Request $request) 30 { 31 return view('person.add'); 32 } 33 34 public function create(Request $request) 35 { 36 $this -> validate($request, Person::$rules); 37 $person = new person; 38 $form = $request->all(); 39 unset($form['_token']); 40 $person->fill($form)->save(); 41 return redirect('/person'); 42 } 43 44 public function edit(Request $request) 45 { 46 $person = Person::find($request-> id); 47 return view('person.edit',['form'=> $person]); 48 } 49 50 public function update(Request $request) 51 { 52 $this -> validate($request, Person::$rules); 53 $person = person::find($request->id); 54 $form = $request->all(); 55 unset($form['_token']); 56 $person->fill($form)->save(); 57 return redirect('/person'); 58 } 59 60 public function delete(Request $request) 61 { 62 $person = Person::find($request->id); 63 return view('person.del',['form'=> $person]); 64 } 65 66 public function remove(Request $request) 67 { 68 person::find($request -> id)->delete(); 69 return redirect('/person'); 70 } 71}
【Board.php】
php
1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6 7class Board extends Model 8{ 9 10 public function person() 11 { 12 return $this->belongsTo('App\Person'); 13 } 14 15 16 protected $guarded = array('id'); 17 18 public static $rules = array( 19 'person_id' => 'required', 20 'title' => 'required', 21 'message' => 'required' 22 ); 23 24 public function getData() 25 { 26 return $this->id . ': ' . $this->title . ' (' . $this->person['name'] . ')'; 27 } 28}
【Person/index.blade.php】
php
1@extends('layouts.helloapp') 2 3@section('title','Person.index') 4 5@section('menubar') 6 @parent 7 インデックスページ 8@endsection 9 10@section('content') 11 <table> 12 <tr> 13 <th>Person</th> 14 <th>Board</th> 15 </tr> 16 @foreach($hasItems as $item) 17 <tr> 18 <td>{{$item->getData()}}</td> 19 <td> 20 <table width="100%"> 21 @foreach ($item ->boards as $obj) 22 <tr> 23 <td> 24 {{ $obj -> getData() }} 25 </td> 26 <tr/> 27 @endforeach 28 </table> 29 </td> 30 </tr> 31 @endforeach 32 </table> 33 <div style="margin:10px"></div> 34 <table> 35 <tr><th>Person</th></tr> 36 @foreach ($noItems as $item) 37 <tr> 38 <td>{{ $item->getData()}}</td> 39 </tr> 40 @endforeach 41 </table> 42@endsection 43 44@section('footer') 45 copyright 2019 yamamoto. 46@endsection
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/20 15:21
2019/02/21 03:48