リレーションで値を取得する
リレーションにリレーションを繋げて値を取得したいです
Model間の関係は
User ModelとComment ModelはhasManyで
User ModelとUserifno ModelはhasOneです
発生している問題・エラーメッセージ
Trying to get property of non-object
該当のソースコード
User.php
php
1<?php 2 3 4namespace App; 5 6use Illuminate\Notifications\Notifiable; 7use Illuminate\Foundation\Auth\User as Authenticatable; 8use App\Userinfo; 9 10class User extends Authenticatable 11{ 12 use Notifiable; 13 use \Conner\Tagging\Taggable; 14 15 /** 16 * The attributes that are mass assignable. 17 * 18 * @var array 19 */ 20 protected $fillable = [ 21 'name', 'account_id', 'email', 'password', 22 ]; 23 24 /** 25 * The attributes that should be hidden for arrays. 26 * 27 * @var array 28 */ 29 protected $hidden = [ 30 'password', 'remember_token', 31 ]; 32 33 public function userinfo() 34 { 35 return $this->hasOne('App\Userinfo'); 36 } 37 38 public function comments() 39 { 40 return $this->hasMany('App\Comment'); 41 } 42}
Userinfo.php
php
1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6use Storage; 7 8class Userinfo extends Model 9{ 10 protected $fillable = [ 11 'user_id', 12 'avatar', 13 'intro', 14 ]; 15 16 public function user() 17 { 18 return $this->belongsTo('App\User')->withDefault([ 19 'avatar' => 'default_avatar.png', 20 ]); 21 } 22 23 public function comments() 24 { 25 return $this->hasMany('App\Comment'); 26 } 27 28 public function getPathAttribute() 29 { 30 $env = config('app.env'); 31 32 if($env === 'local'){ 33 $path = asset('storage/avatars/'.$this->avatar); 34 }else if($env === ('test' || 'production')){ 35 $path = Storage::disk('s3')->url('avatars/'.$this->avatar); 36 } 37 38 return $path; 39 } 40}
Comment.php
php
1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6use Carbon\Carbon; 7 8 9class Comment extends Model 10{ 11 protected $fillable = [ 12 'page_id', 13 'user_id', 14 'type', 15 'body', 16 ]; 17 18 public function page() 19 { 20 return $this->belongsTo('App\Page'); 21 } 22 23 public function user() 24 { 25 return $this->belongsTo('App\User')->withDefault([ 26 'name' => 'ゲスト', 27 ]); 28 } 29}
PageController.php
php
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use Illuminate\Support\Facades\Auth; 7use Illuminate\Validation\Rule; 8use App\Http\Requests\StorePageRequest; 9use App\Library\StoreImage; 10use App\Book; 11use App\Page; 12use App\Comment; 13 14 15class PageController extends Controller 16{ 17 public function show(Page $page) 18 { 19 $page->with([ 20 'comments.replies', 21 'comments.user', 22 'comments' 23 ])->get(); 24 25 $book = $page->book; 26 27 return view('pages.show',[ 28 'page' => $page, 29 'book' => $book, 30 ]); 31 } 32}
show.blade.php
php
1@extends('default_layouts.app') 2 3@section('title') 4{{ $book->title }}の{{ $page->page }}ページについて | {{ config('app.name') }} 5@endsection 6 7@section('content') 8@forelse($page->comments as $comment) 9<img src="{{ $comment->user->userinfo->path }}"> 10@endforelse 11@endsection
試したこと
{{ $comment->user->userinfo->path }}の部分がエラーになりますが
{{ dd($comment->user->userifno->path) }}とすると値が取得できます
また
User Model 単体からUserinfo Modelへのリレーションは上手くいきます
$user = User::find(1);
$user->userinfo->pathでは値を取得できます
補足情報(FW/ツールのバージョンなど)
Laravel 5.7
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。