前提・実現したいこと
以下のような多次元連想配列をビューで表示したいです
エラーメッセージ
Undefined variable: user (View: /Users/appleiii/Programming/php/fake/src/resources/views/home.blade.php)
該当のソースコード
php
1 public function index() 2 { 3 $all_posts = []; 4 $posts = Post::latest()->get(); 5 6 foreach($posts as $post){ 7 $photos = $post->photos; 8 $u_id = $post->u_id; 9 $user = User::find($u_id); 10 11 $photo_posts = array( 12 'user' => $user, 13 'post' => $post, 14 'photos' => $photos, 15 ); 16 17 $all_posts[] = $photo_posts; 18 } 19 return view('home')->with('all_posts',$all_posts); 20 }
php
1 @foreach($all_posts as $photo_post) 2 <div class="card"> 3 {{ $photo_post->$user->id }} 4 </div> 5 @endforeach
Post.php
php
1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6 7class Post extends Model 8{ 9 10 protected $fillable = [ 11 'u_id', 'comment', 12 ]; 13 14 public function user() 15 { 16 return $this->belongsTo('App\User'); 17 } 18 19 public function photos() 20 { 21 return $this->hasMany('App\Photo'); 22 } 23} 24
試したこと
様々な書き方を試してみましたが、表示できませんでした
php
1{{ $photo_post->user->id }} 2{{ $user->id}}
補足情報(FW/ツールのバージョンなど)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/03/06 16:04 編集
退会済みユーザー
2019/03/06 16:12
退会済みユーザー
2019/03/06 16:18
退会済みユーザー
2019/03/06 16:22
退会済みユーザー
2019/03/06 16:34