2013-05-29 3 views
0

나는 다음과 같은 경로가 있습니다모델 관계 및 노선들은 모델 ID를 얻을

Route::get('notes/main', function(){ 

     $destinations = Destination::where('show', '=','1')->get(); 
     $notes = Destination::find($destination->id)->notes()->get(); 
     return View::make('notes.main') 
     ->with('destinations', $destinations); 

}); 

// 관계 모델 :

<?php 
class Destination extends Eloquent { 
    public function notes() 
    { 
    return $this->has_many('Note'); 
    } 
} 

<?php 
class Note extends Eloquent 

{ 
     public function destination() 
     { 
      return $this->belongs_to('Destination'); 
     } 

} 

//보기 : 올바른 무엇

@foreach($destinations as $destination) 

{{ $destination->name}}<br> 
{ $notes->destination->text }} // this isn't echoed 
@endforeach 

을 방법을 필터링하고 $ destination-> id를 정의하는 방법

감사합니다

루프 내 if 문에서 메모를 어떻게 필터링합니까? @if (는 isset ($ 노트 -> 제목)! = 'shorttext') 경로에서

  @else 
      <p> {{ $note->text }} </p> 
      @endif 
      @endforeach 

답변

-1

OK 이 말 함수는 isset 밖으로 근무하고 빈 이상한 작동하지 않았다

@if ($note->title == 'shorttext') 
0

는, 당신은 $ 뷰에 변수를 지적 전송하지.

나는 그것을 할 것입니다 방법 :

다음
$destinations = Destination::where('show', '=','1')->get(); 
    $destinations->notes = Destination::find($destination->id)->notes()->get(); 

보기 :

@foreach($destinations as $destination) 
    {{ $destination->name}}<br> 
    {{ $destination->notes->text }} 
@endforeach 
+0

내가 알기로 : 정의되지 않은 변수 : 대상 – dflow

1

당신은 당신의 경로에 $ destination-> ID를 사용하지만 아직 정의되지 않은 것 같습니다. 문제는 코드로 무엇을 달성하고 싶습니까? . 는로 -> 첫 번째() 첫 번째를 얻기 위해, 또는

$destinations = Destination::where('show', '=','1')->get(); 
$notes = Destination::find($destination->id)->notes()->get(); 

모든() (지금까지, $ 대상이 정의되지 않은 당신은 대상 :: 사용할 수있는 하나 개의 특정 대상에만주의를 얻고있다 Destination :: find (id). ID는 필요한 대상의 기본 키 값으로 바뀝니다.

그러나 나는 그런 식으로하고 싶지 않다고 생각합니다. 출력에서 각 대상 및 각 대상 아래에 해당 노트가 출력되도록하려는 것 같습니다.

컨트롤러 :

//gets all the destinations 
$destinations = Destination::where('show', '=','1')->get(); 
return View::make('notes.main') 
    ->with('destinations', $destinations); 

보기 :

@foreach($destinations as $destination) 
    {{ $destination->name}}<br> 
    @foreach($destination->notes as $note) 
    {{ $note->text }} <br> 
    @endforeach 
    <hr> 
@endforeach 

이를 테스트하지 않았지만,이 방법은 당신의보기는 당신에게 당신의 목적지를 표시하고, 각 대상에 대한 모든 노트 것

. 마지막 부분에 대한

+0

감사합니다 Matthias, 유일한 : – dflow

+0

당신의 의견은 잘린 것처럼 보이지만 나는 결과물을 실수로 보았습니다. $ destination-> notes-> text가 아니라 $ note-> text 여야합니다. 나는 그것을 적절하게 편집 할 것이다. –

0

그래서 먼저 내가 당신에게 정확한 답을 준, 질문을. 답변을 수락 한 것으로 표시했지만 나중에 처음 질문을 편집하여 다른 질문을 추가하십시오 (대신 새 스레드를 만들어야 함). 그러면 초기 질문의 일부에만 답하는 자신 만의 답을 만들고 그 답을 좋은 해결책으로 표시 하시겠습니까?

왜 내가 당신의 인형과 비어가 작동하지 않았는 지 말할 수 있었지만, 내 도움이 전혀 가치가 없다면 왜해야합니까?