2016-07-12 3 views
0

나는 그들의 관련 파일과 주소와 함께 모든 속성을로드하는 열망로드를 사용laravel 5.2 내 컨트롤러에서

public function search_results(Request $request) 
{ 


    $Properties = Property::with('Residential', 'File', 'addresses')->get(); 

    return view('result', ['results' => $Properties]); 
} 

및 패스 바로보기로한다. 그러나 view.blade.php에서이 관련 데이터에 어떻게 액세스합니까?

내보기의 예입니다. 그러나 관련 데이터와 관련하여 작동하지 않는 것 같습니다.

 @forelse ($results as $result) 
<div class="row"> 
    <div class="col-md-7"> 
     @foreach($result->File() as $file) 
     <a href="#"> 
      <img class="img-responsive" alt="" src="{{ URL::to('/') }}/images/properties/{{$file->filename}}"> 
     </a> 
     @endforeach 
    </div> 
    <div class="col-md-5"> 
     <h3>{{ $result->title }}</h3> 
     <h4>Subheading</h4> 
     <p>{{ $result->description }}</p> 
     <a class="btn btn-primary" href="#">View Property 
      <span class="glyphicon glyphicon-chevron-right"></span> 
     </a> 
    </div> 
</div> 

이 내 재산 모델입니다 :

class Property extends Model 
{ 


protected $table = 'properties'; 
/** 
* The attributes that are mass assignable. 
* 
* @var array 
*/ 
protected $fillable = [ 
    'user_id', 'is_for_sale','available','title', 'description','price','date_available' 
]; 

public function user() 
{ 
    return $this->belongsTo('App\User'); 
} 


public function Residential() 
{ 
    return $this->hasOne('App\Residential'); 
} 

/** 
* The Addresses that belong to the properties. 
*/ 
public function addresses() 
{ 
    return $this->belongsToMany('App\Address'); 
} 


public function File() 
{ 
    return $this->hasMany('App\File'); 
} 
} 

파일 모델

class File extends Model 
    { 

protected $table = 'files'; 
/** 
* The attributes that are mass assignable. 
* 
* @var array 
*/ 
protected $fillable = [ 
    'property_id', 'file_type_id','filename','system_filename', 'is-deleted' 
]; 


public function Property() 
{ 
    return $this->belongsTo('App\Property'); 
} 


public function File_Type() 
{ 
    return $this->belongsTo('App\File_types'); 
} 
} 

뷰에서 관련 데이터의 유형을 처리하는 올바른 방법은 무엇입니까?

@foreach($result->File as $file)

@foreach($result->File() as $file)

변화 그것을

+0

"내보기의 예가 있지만 관련 데이터와 관련하여 작동하지 않는 것 같습니다."라고 표시되면 작동하지 않는 것은 무엇입니까? 데이터 표시? 먼저'dd ($ Properties)'를 시도하고 무엇이 뷰로 보내지는지보십시오. –

+0

속성 개체는 특성 속성을 포함해야하며, 또한 주거 ','파일 '및'주소 '개체가있는 관계라는 배열을 포함합니다. 그래서보기에 올바르게 전송 된 것처럼 보입니다. 나를 위해 일하지 않는 무엇이 전망 안쪽에서 관계에 접근하고있다. $ result-> title이 작동하고 속성의 제목을 표시하지만 관련 파일 객체를로드하려고하면 치명적인 오류가 발생합니다. – user794846

+0

내가 물어 보는 것은 내가 객체와 관련 객체의로드 목록을 열망하는 것입니다. 블레이드 템플릿 내부에서 관련 객체에 어떻게 액세스합니까? $ result-> File()은 $ 파일로 맞습니까? – user794846

답변

0

.

$result->File()은 컨트롤러의 search_results() 메서드와 동일한 관계를로드합니다.