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