0
Expensive,
You can help me with this mistake:
local.ERROR: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`clima`.`tbl_consenso`, CONSTRAINT `tbl_consenso_opcao_id_foreign` FOREIGN KEY (`opcao_id`) REFERENCES `opcoes` (`id`))
Consequently it returns me the error ERR_TOO_MANY_REDIRECTS
in the view.
I’ve checked all Foreign key relationships.
Excerpt controller Store
<?php
public function store( Request $request, Pesquisa $pesquisa )
{
$this->guardaAvaliacao( $pesquisa );
$opcoes = $request->input( 'pergunta', [] );
$opcoes = Opcao::findMany( $opcoes );
$pesquisa->load( 'consensos' );
collect( $request->input( 'pergunta', [] ) )
->each( function ( $opcaoId, $perguntaId ) use ( $request, $pesquisa, $opcoes ) {
$opcaoId = intval( $opcaoId );
$perguntaId = intval( $perguntaId );
$opcao = $opcoes->whereStrict( 'id', $opcaoId )->first();
$consenso = $pesquisa->consensos->whereStrict( 'pergunta_id', $perguntaId )->first();
Consenso::forceCreate( [
'user_id' => $request->authUser()->id,
'matricula' => $pesquisa->matricula,
'pesquisa_id' => $pesquisa->id,
'pergunta_id' => $perguntaId,
'opcao_id' => $opcao->id,
'lookup_regime_contratacao_id' => $pesquisa->lookup_regime_contratacao_id,
'lookup_area_atuacao_id' => $pesquisa->lookup_area_atuacao_id,
'lookup_cargo_id' => $pesquisa->lookup_cargo_id,
'lookup_gestor_id' => $pesquisa->lookup_gestor_id,
'lookup_tempo_empresa_id' => $pesquisa->lookup_tempo_empresa_id,
'lookup_sexo_id' => $pesquisa->lookup_sexo_id,
'lookup_escolaridade_id' => $pesquisa->lookup_escolaridade_id,
'nota_original' => $consenso->nota_original,
'nota_supervisor' => $consenso->nota_supervisor,
'nota_consenso' => $opcao->nota,
'conteudo' => $opcao->descricao,
] );
} );
flash_success( 'Salva com sucesso' );
return redirect()->route( 'clima.consenso.index' );
}
protected function guardaAvaliacao( Pesquisa $pesquisa )
{
$jaFeito = Consenso::where( 'pesquisa_id', $pesquisa->id )->count() > 0;
if ($jaFeito) {
throw new UnauthorizedException( 'Pesquisa ja avalida' );
}
}
Model.
<?php
namespace App\Models\Clima;
use Illuminate\Database\Eloquent\Model;
use App\Presenters\Presentable;
// use App\Models\Model;
use App\Models\ACL\User;
class Consenso extends Model
{
use Presentable;
protected $connection = 'clima';
protected $table = 'tbl_consenso';
protected $casts = [
'id' => 'integer',
'user_id' => 'integer',
'matricula' => 'integer',
'pesquisa_id' => 'integer',
'pergunta_id' => 'integer',
'opcao_id' => 'integer',
'lookup_regime_contratacao_id' => 'integer',
'lookup_area_atuacao_id' => 'integer',
'lookup_cargo_id' => 'integer',
'lookup_gestor_id' => 'integer',
'lookup_tempo_empresa_id' => 'integer',
'lookup_sexo_id' => 'integer',
'lookup_escolaridade_id' => 'integer',
'nota_original' => 'integer',
'nota_supervisor' => 'integer',
'nota_consenso' => 'integer',
'conteudo' => 'string',
];
//Relacionamentos
public function setConteudoAttribute( $value )
{
$this->attributes[ 'conteudo' ] = value_or_null( $value );
}
public function pergunta()
{
return $this->belongsTo( Pergunta::class, 'pergunta_id', 'id' );
}
public function pesquisa()
{
return $this->belongsTo( Pesquisa::class, 'pesquisa_id', 'id' );
}
public function opcao()
{
return $this->belongsTo( Opcao::class, 'opcao_id', 'id' );
}
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
public function regimeContratacao()
{
return $this->belongsTo(LookupRegimeContratacao::class, 'lookup_regime_contratacao_id', 'id');
}
public function avaliacaosups()
{
return $this->belongsTo(AvSupervisor::class, 'pesquisa_id', 'pesquisa_id');
}
public function scopeOrdered(Builder $builder)
{
$builder->orderBy('created_at');
return $builder;
}
public static function avaliacao(Pesquisa $pesquisa)
{
return self::whereNotIn('id', function ($query) use ($pesquisa) {
$query->selectRaw('tbl_resposta_super.pergunta_id')
->from('tbl_resposta_super')
->join('tbl_respostas', 'tbl_resposta_super.pesquisa_id', '=', 'tbl_respostas.pesquisa_id')
->whereRaw('tbl_resposta_super.pesquisa_id=' . $pesquisa->id);
})->ordered()->first();
}
}
Migration.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateConsensoTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::connection( 'clima' )->create('tbl_consenso', function (Blueprint $table) {
$table->increments( 'id' );
$table->unsignedInteger( 'user_id' );
$table->unsignedInteger( 'matricula' );
$table->unsignedInteger( 'pesquisa_id' );
$table->unsignedInteger( 'pergunta_id' );
$table->unsignedInteger( 'opcao_id' );
$table->unsignedInteger( 'lookup_regime_contratacao_id' );
$table->unsignedInteger( 'lookup_area_atuacao_id' );
$table->unsignedInteger( 'lookup_cargo_id' );
$table->unsignedInteger( 'lookup_gestor_id' );
$table->unsignedInteger( 'lookup_tempo_empresa_id' );
$table->unsignedInteger( 'lookup_sexo_id' );
$table->unsignedInteger( 'lookup_escolaridade_id' );
$table->unsignedInteger('nota_original' );
$table->unsignedInteger('nota_supervisor' );
$table->unsignedInteger('nota_consenso' );
$table->text( 'conteudo' )->nullable()->default( null );
$table->timestamps();
$table->unique( [ 'pesquisa_id', 'pergunta_id', 'opcao_id' ] );
$table->foreign( 'pesquisa_id' )->references( 'id' )->on( 'pesquisas' );
$table->foreign( 'pergunta_id' )->references( 'id' )->on( 'perguntas' );
$table->foreign( 'opcao_id' )->references( 'id' )->on( 'opcoes' );
$table->foreign( 'lookup_regime_contratacao_id' )->references( 'id' )->on( 'lookup_regimes_contratacao' );
$table->foreign( 'lookup_area_atuacao_id' )->references( 'id' )->on( 'lookup_areas_atuacao' );
$table->foreign( 'lookup_cargo_id' )->references( 'id' )->on( 'lookup_cargos' );
$table->foreign( 'lookup_gestor_id' )->references( 'id' )->on( 'lookup_gestores' );
$table->foreign( 'lookup_tempo_empresa_id' )->references( 'id' )->on( 'lookup_tempos_empresa' );
$table->foreign( 'lookup_sexo_id' )->references( 'id' )->on( 'lookup_sexos' );
$table->foreign( 'lookup_escolaridade_id' )->references( 'id' )->on( 'lookup_escolaridades' );
} );
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tbl_consenso');
}
}
Route.
/** @var \Illuminate\Routing\Router $router */
$router->group( [ 'prefix' => 'avaliacao', 'namespace' => 'Clima', 'as' => 'clima.' ],
function ( $router ) {
/** @var \Illuminate\Routing\Router $router */
$router->get( '/', [ 'uses' => 'ClimaController@redireciona', 'as' => 'redireciona' ] );
$router->get( 'inicio', [ 'uses' => 'ClimaController@inicio', 'as' => 'inicio' ] );
$router->get( 'obrigado', [ 'uses' => 'ClimaController@obrigado', 'as' => 'obrigado' ] );
$router->get( 'sobre-voce', [ 'uses' => 'ClimaController@sobre', 'as' => 'sobre' ] );
$router->post( 'sobre-voce', [ 'uses' => 'ClimaController@cria', 'as' => 'cria' ] );
$router->get( 'pergunta', [ 'uses' => 'ClimaController@pergunta', 'as' => 'pergunta' ] );
$router->post( 'pergunta/{id}', [ 'uses' => 'ClimaController@salva', 'as' => 'salva' ] );
//Grupo Supervisor
$router->group( [ 'prefix' => 'supervisor', 'as' => 'supervisor.' ],
function ( $router ) {
/** @var \Illuminate\Routing\Router $router */
$router->get( '/', [ 'uses' => 'SupervisorController@index', 'as' => 'index' ] );
$router->get( '/pesquisa/{clima_pesquisa}', [
'uses' => 'SupervisorController@show',
'as' => 'show'
] );
$router->post( '/pesquisa/{clima_pesquisa}', [
'uses' => 'SupervisorController@store',
'as' => 'store'
] );
}
);
//Grupo consenso
$router->group( [ 'prefix' => 'consenso', 'as' => 'consenso.' ],
function ( $router ) {
/** @var \Illuminate\Routing\Router $router */
$router->get( '/', [ 'uses' => 'ConsensoController@index', 'as' => 'index' ] );
$router->get( '/pesquisa/{clima_pesquisa}', [
'uses' => 'ConsensoController@show',
'as' => 'show'
] );
$router->post( '/pesquisa/{clima_pesquisa}', [
'uses' => 'ConsensoController@store',
'as' => 'store'
] );
}
);
}
);
try changing in Migration the Foreign q is giving error to: $table->Foreign('field')->References('id')->on('table')->onUpdate('Cascade')->onDelete('Cascade')
– Ademilson Santana da Silva
Sorry for the lack of information, I added the complete error.
– Ronaldo Adriano