2
I need to insert users into my database MySQL
already with the relationship N:M
, but I don’t know how to do it.
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);
}
}
Insert the user, take the generated id. Insert the routes, take the generated ids. Insert all in users_routes.
– Woss
how will I insert everything into users_routes ?
– Joan Marcos
ready edited the post
– Joan Marcos
@Andersoncarloswoss, or create a database database that receives a new user and new routes and makes this connection
– Jefferson Quesado
ah blz man, I’ll do a search
– Joan Marcos
I wrote these days as it should be done for the standard convention, if you have already missed the name of the intermediate table that should be rotated ...
– novic
Related: https://answall.com/questions/274497/como-name-tabela-pivot-many-to-many/274509#274509
– novic