I have a season, and each clan has to have a valid season entry. The stage I'm currently stuck on is how can I have a relationship for clans in my Season class?
This would help me understand which clans a season currently holds, but without a complicated query, it seems impossible with relationships. Is there a 'good practice' way of doing this?
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Season extends Model
{
public function matches() {
return $this->hasMany('App\ClanMatch', 'season_id');
}
public function tournaments() {
return $this->hasMany('App\Tournament', 'season_id');
}
}
class SeasonEntry extends Model
{
public function season() {
return $this->belongsTo('App\Season', 'season_id');
}
public function clan() {
return $this->belongsTo('App\Clan', 'clan_id');
}
}
Think if simple.
So I guess, this is Many-to-Many
relationship. To deal with it in Laravel, commonly, you need to create a pivot table that defines the co-relation.
Schema::create('season_clan', function (Blueprint $table) {
$table->increments('id');
$table->integer('season_id')->unsigned();
$table->integer('clan_id')->unsigned();
});
And both models need to address the relationship as well.
class Season extends Model
{
public function clans()
{
return $this->belongsToMany(Clan::class);
}
}
class Clan extends Model
{
public function seasons()
{
return $this->belongsToMany(Seasons::class);
}
}
That's all I guess! You can try and it should work.