doutao1939 2014-05-13 07:45
浏览 45

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

I've just started using Laravel and I am really enjoying it. I am a little confused about how to access data at times and i'm not sure if I am going around this in the right way, Maybe somebody can help? I want to simply output the Club Name and Opponent Club Name in a loop of Fixtures. I have the following tables:

// 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();
});

So a CLUB can have MANY FIXTURES and a FIXTURE can have MANY CLUBS. My Club model is as follows:

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

My Fixture model is as follows:

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

Then in my controller I use Elequent ORM to get the upcoming fixtures

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 hope I make sense with all this. I've tried to separate out the data, trying to keep the clean modular and dry, but I've immediately hit a problem here. Hopefully a Laravel guru can steer me in the right direction here? Thanks

  • 写回答

1条回答 默认 最新

  • dongzhuo2010 2014-05-13 10:19
    关注

    You're doing it wrong, first you don't need to implement UserInterface and RemindableInterface (but keep it in User model ), second you can add relation clubs() inside your Fixture model

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

    Then in your controller

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大