2014-05-13 2 views
0

방금 ​​Laravel을 사용하기 시작했으며 정말 즐겁습니다. 약간의 시간에 데이터에 액세스하는 방법에 대해 약간 혼란스럽고 나는 이것을 올바른 방향으로 돌리고 있는지 확실하지 않습니다. 아마도 누군가가 도울 수 있습니까? 클럽 이름과 상대방 클럽 이름을 경기 일정에 따라 간단하게 출력하고 싶습니다. Laravel : 피벗 테이블에서 데이터 가져 오기

// a clubs table 
Schema::create('clubs', function($table) { 
    $table->increments('id'); 
    $table->string('name', 20); 
    $table->string('code', 40); 
    $table->string('location', 20); 
    $table->string('colour', 20); 
    $table->string('alias', 20); 
    $table->string('image', 200); 
    $table->timestamps(); 
}); 

// a fixtures table 
Schema::create('fixtures', function($table) { 
    $table->increments('id'); 
    $table->string('type', 20)->nullable(); 
    $table->string('date', 20)->nullable(); 
    $table->string('time', 20)->nullable(); 
    $table->string('venue', 20)->nullable(); 
    $table->string('address', 20)->nullable(); 
    $table->boolean('reminders')->nullable(); 
    $table->timestamps(); 
}); 

// a pivot table 
Schema::create('clubs_fixtures', function($table) { 
    $table->increments('id'); 
    $table->integer('club_id')->unsigned(); // this is meant to be used as a foreign key 
    $table->foreign('club_id')->references('id')->on('clubs')->onDelete('cascade')->onUpdate('cascade'); 
    $table->integer('opponent_id')->unsigned(); // this is meant to be used as a foreign key 
    $table->foreign('opponent_id')->references('id')->on('clubs')->onDelete('cascade')->onUpdate('cascade'); 
    $table->integer('fixture_id')->unsigned(); // this is meant to be used as a foreign key 
    $table->foreign('fixture_id')->references('id')->on('fixtures')->onDelete('cascade')->onUpdate('cascade'); 
    $table->timestamps(); 
}); 

그래서 클럽이 많은 비품을 가질 수와 FIXTURE가 많은 클럽을 가질 수 나는 다음과 같은 테이블이있다. 내 클럽의 모델은 다음과 같다 :

use Illuminate\Auth\UserInterface; 
use Illuminate\Auth\Reminders\RemindableInterface; 
class Club extends Eloquent implements UserInterface, RemindableInterface { 
    protected $table = 'clubs'; 
    public function getAuthIdentifier() { 
     return $this->getKey(); 
    } 
    public function getAuthPassword() { 
     return $this->password; 
    } 
    public function getRememberToken() { 
     return $this->remember_token; 
    } 
    public function setRememberToken($value) { 
     $this->remember_token = $value; 
    } 
    public function getRememberTokenName() { 
     return 'remember_token'; 
    } 
    public function getReminderEmail() { 
     return $this->email; 
    } 
    // set validation 
    public static $createUpdateRules = array(
     'name'=>'required|min:2', 
     'location'=>'required|min:2', 
     'colour'=>'required|min:2', 
     'alias'=>'required|min:2' 
    ); 
    // We can define a many-to-many relation using the belongsToMany method: 
    public function fixtures() { 
     return $this->belongsToMany('Fixture', 'clubs_fixtures')->withPivot('opponent_id'); 
    } 
} 

내 고정 장치 모델은 다음과 같다 :

use Illuminate\Auth\UserInterface; 
use Illuminate\Auth\Reminders\RemindableInterface; 
class Fixture extends Eloquent implements UserInterface, RemindableInterface { 
    protected $table = 'fixtures'; 
    public function getAuthIdentifier() { 
     return $this->getKey(); 
    } 
    public function getAuthPassword() { 
     return $this->password; 
    } 
    public function getRememberToken() { 
     return $this->remember_token; 
    } 
    public function setRememberToken($value) { 
     $this->remember_token = $value; 
    } 
    public function getRememberTokenName() { 
     return 'remember_token'; 
    } 
    public function getReminderEmail() { 
     return $this->email; 
    } 
    // set validation 
    public static $createRules = array(
     'type'=>'required|min:2', 
     'opponent'=>'required|min:1', 
     'date'=>'required|min:2', 
     'time'=>'required|min:2', 
     'venue'=>'required|min:2', 
     'address'=>'required|min:2', 
     'reminders'=>'required' 
    ); 
} 

그런 다음 내 컨트롤러에서 나는 Elequent ORM이 곧 비품을

public function getViewClub($id) { 
    $upcomingFixtures = Club::find($id)->fixtures()->where('date', '>=', new DateTime('today'))->get(); 
    return View::make('club.view')->with('upcomingFixtures', $upcomingFixtures); 
} 

@foreach($upcomingFixtures as $upcomingFixture) 
    <p> 
     <a href="{{ URL::to('dashboard/'.$club->id.'/fixtures/upcoming/'.$upcomingFixture->id) }}"> 
      <strong>{{ $club->name }} vs Team Name</strong> 
     </a> 
    </p> 
@endforeach 

I를 얻기 위해 사용 나는이 모든 것을 이해할 수 있기를 바랍니다. 나는 데이터를 분리하려고 노력했다. 모듈러하고 건조한 상태로 유지하려고 노력했지만, 여기서 바로 문제가 발생했다. Laravel 전문가가 올바른 방향으로 나를 조종 할 수 있기를 바랍니다. 감사

답변

0

당신은 두 번째 당신이 당신의 Fixture 모델 내부() 관련 클럽을 추가 할 수 있습니다, 먼저 UserInterfaceRemindableInterface를 구현하는 (그러나 사용자 모델에 보관) 할 필요가 없습니다, 잘못을하고있는

class Fixture extends Eloquent { 
protected $table = 'fixtures'; // you can also remove this line 
    // set validation 
    public static $createRules = array(
     'type'=>'required|min:2', 
     //.... 
    ); 
    public function clubs() { 
      return $this->belongsToMany('Club','clubs_fixtures') 
        ->withPivot('opponent_id'); 
    } 

} 

컨트롤러에서

public function getViewClub($id) { 
    $upcomingFixtures = Fixture::with('clubs') 
        ->where('date', '>=', new DateTime('today'))->get(); 
    return View::make('club.view',compact('upcomingFixtures')); 
} 
@foreach($upcomingFixtures as $upcomingFixture) 
    <h1>{{ $upcomingFixture->type }}</h1> 
    @foreach($upcomingFixture->clubs as $club) 
     <p> 
      <a href="{{ URL::to('dashboard/'.$club->id.'/fixtures/upcoming/'.$upcomingFixture->id) }}"> 
      <strong>{{ $club->name }} vs Team Name</strong> 
      </a> 
     </p> 
     <p> access to pivot table: {{ $club->pivot->opponent_id }}</p> 
    @endforeach 
@endforeach 
관련 문제