Skip to content

Instantly share code, notes, and snippets.

@brunofonseca
Created December 9, 2015 19:35
Show Gist options
  • Save brunofonseca/65b3955324b9669df2a1 to your computer and use it in GitHub Desktop.
Save brunofonseca/65b3955324b9669df2a1 to your computer and use it in GitHub Desktop.
// Controller
public function store(Request $request)
{
$input = $request->all();
$curso = new Curso();
$curso->nome_curso = $input['nome_curso'];
$curso->conteudo_programatico_curso = $input['conteudo_programatico_curso'];
$curso->data_inicio_curso = $input['data_inicio_curso'];
$curso->data_fim_curso = $input['data_fim_curso'];
$curso->status_curso = $input['status_curso'];
$curso->save();
$lotes_id = Input::get('lotes_id');
$curso->lotes()->sync($lotes_id)->save();
//$curso->lotes()->attach($lotes_id)->save();
}
//Model Curso
class Curso extends Model
{
protected $table = 'cursos';
protected $fillable = ['nome_curso',
'conteudo_programatico_curso',
'data_inicio_curso',
'data_fim_curso',
'status_curso'
];
protected $hidden = [];
public function lotes(){
return $this->belongsToMany('App\Lote', 'curso_lotes', 'lote_id', 'curso_id');
}
}
// Mode Lote
class Lote extends Model
{
protected $table = 'lotes';
protected $fillable = [];
protected $hidden = [];
public function cursos(){
return $this->belongsToMany('App\Curso', 'curso_lotes', 'curso_id', 'lote_id');
}
}
// Tabela de curso
class CreateTableCursos extends Migration
{
public function up()
{
Schema::create('cursos', function (Blueprint $table) {
$table->increments('id');
$table->string('nome_curso');
$table->longText('conteudo_programatico_curso');
$table->dateTime('data_inicio_curso');
$table->dateTime('data_fim_curso');
$table->integer('status_curso');
$table->timestamps();
});
}
public function down()
{
Schema::drop('cursos');
}
}
// Tabela de Lotes
class CreateTableLotes extends Migration
{
public function up()
{
Schema::create('lotes', function (Blueprint $table) {
$table->increments('id');
$table->string('nome_lote');
$table->float('valor_lote');
$table->integer('quantidade_lote');
$table->integer('status_lote'); // 0 - Inativo / 1 - Ativo
$table->timestamps();
});
}
public function down()
{
Schema::drop('lotes');
}
}
// Tabela curso_lotes
class CreateTableCursoLotes extends Migration
{
public function up()
{
Schema::create('curso_lotes', function(Blueprint $table){
// Keys
$table->increments('id');
$table->unsignedInteger('curso_id')->nullable();
$table->unsignedInteger('lote_id')->nullable();
// Fk's
$table->foreign('curso_id')
->references('id')
->on('cursos')
->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('lote_id')
->references('id')
->on('lotes')
->onDelete('cascade')
->onUpdate('cascade');
});
}
public function down()
{
Schema::drop('curso_lotes');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment