I need to insert users into my database the MySQL
data already with the N:M
relationship, but I do not know how to do that.
Migrate:
Schema::create('usuarios', function (Blueprint $table) {
$table->increments('id');
$table->string('nome');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Schema::create('rotas', function (Blueprint $table) {
$table->increments('id');
$table->string('action');
$table->timestamps();
});
Schema::create('usuarios_rotas', function (Blueprint $table) {
$table->integer('usuario_id')->unsigned();
$table->foreign('usuario_id')->references('id')->on('usuarios');
$table->integer('rota_id')->unsigned();
$table->foreign('rota_id')->references('id')->on('rotas');
});
Model:
class Usuario extends Model
{
protected $fillable = [
'nome', 'email','password'
];
public function rotas() {
return $this->belongsToMany(Rota::class);
}
}
class Rota extends Model
{
protected $fillable = [
'rota'
];
public function usuarios() {
return $this->belongsToMany(Usuario::class);
}
}