簡體   English   中英

Laravel:從數據透視表中獲取數據

[英]Laravel: Getting data from my pivot table

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

因此,一個俱樂部可以有很多固定裝置,而一個夾具可以有許多固定裝置。 我的俱樂部模型如下:

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

我希望我對所有這些都有意義。 我試圖分離出數據,試圖保持干凈的模塊化和干燥,但是我立即在這里遇到了問題。 希望Laravel專家可以在這里指引我正確的方向嗎? 謝謝

您做錯了,首先不需要實現UserInterfaceRemindableInterface (但將其保留在User模型中),其次可以在Fixture模型內添加關系clubs()

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM