I've got a problem/question regarding belongsToMany.
I've got three tables, the one table "tableone" stores regions for another table, the "tableone" table has a fixed number of records and it might increase or decline but not as much as "tabletwo", my "tabletwo" have multiple sizes and each size can have many regions that it belongs to.
My "tableone" sql looks as follows:
| Field | Type | Null | Key | Default | Extra
| ----------- | ----------------- | ----- | ----- | ----------------- | ------------------
| id | int(10) unsigned | NO | PRI | NULL | auto_increment
| name | varchar(20) | NO | | NULL |
| slug | varchar(10) | NO | MUL | NULL |
| available | tinyint(1) | NO | | NULL |
My "tabletwo" sql looks as follows:
+---------------+---------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------------+------+-----+---------------------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| type | varchar(10) | NO | | NULL | |
| distribution | varchar(10) | NO | | NULL | |
| slug | varchar(10) | NO | MUL | NULL | |
| public | tinyint(1) | NO | | NULL | |
| min_disk_size | varchar(5) | NO | | NULL | |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
+---------------+---------------------+------+-----+---------------------+----------------+
And my mapping table looks as follows:
+---------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+-------+
| foreign1_id | int(10) unsigned | NO | MUL | NULL | |
| foreign2_id | int(10) unsigned | NO | MUL | NULL | |
+---------------+------------------+------+-----+---------+-------+
And my models looks like this:
class TableOne extends Model {
protected $table = 'tableone';
public $timestamps = false;
protected $guarded = ['id'];
protected $fillable = ['name', 'slug', 'available'];
public function sizes()
{
return $this->belongsToMany('App\TableOne', 'maps', 'foreign1_id', 'foreign2_id');
}
}
class TableTwo extends Model {
protected $table = 'tabletwo';
public $timestamps = true;
protected $fillable = ['id', 'name', 'distribution', 'slug', 'public', 'created_at', 'min_disk_size'];
public function regions()
{
return $this->belongsToMany('App\TableTwo', 'maps', 'foreign2_id', 'foreign1_id');
}
}
So I use them as follows to call data,
$TableTwo = TableTwo::find(12950274);
foreach ($TableTwo->regions()->get() as $region)
{
echo($region->slug) . "\n";
}
Attaching data
$TableOne = TableOne::find(9);
$TableTwo = TableTwo::find(10);
$TableOne->sizes->attach($TableTwo);
$TableOne->save();
Detaching data
$TableOne = TableOne::find(9);
$TableTwo = TableTwo::find(10);
$TableOne->sizes->detach($TableTwo);
$TableOne->save();
So I guess my question would be, am I using the code in the correct format, is there anyway of doing things because at the moment a lot of queries are taking place to attach data.
Any Help or just feedback, questions about what I'm trying to do would be great just to point me in the right direction or to help me to figure out the logic behind all this.
Another thing is I'm using eloquent outside laravel because the package I'm developing for was not written in laravel "which I HATE" but its a paid system, so I can really tell the developers, Hey get with the time.
Please and thank you.