Laravel:从我的数据透视表中获取数据

时间:2014-05-13 07:45:59

标签: php laravel blade

我刚刚开始使用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();
});

所以CLUB可以拥有多个FIXTURES,而FIXTURE可以有很多CLUBS。 我的俱乐部模型如下:

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');
    }
}

我的Fixture模型如下:

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

我希望我能理解这一切。我试图将数据分开,试图保持干净的模块化和干燥,但我在这里立刻遇到了问题。 希望Laravel大师可以引导我朝着正确的方向前进吗? 感谢

1 个答案:

答案 0 :(得分:0)

你做错了,首先你不需要实现UserInterfaceRemindableInterface(但保留在用户模型中),其次你可以添加关系俱乐部()在Fixture模型中

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