Error while trying to Insert into database with Laravel

Asked

Viewed 346 times

0

I’m using the Laravel and I’ve been racking my brain trying to figure out the cause of the following mistake while trying to make a insert:

(1/1) Errorexception Illegal offset type

in Hasattributes.php (line 818)

Handleexceptions->handleError(2, 'Illegal offset type', 'C: nisfram vendor Laravel framework src Illuminate Database Eloquent Concerns Hasattributes.php', 818, array()) in Hasattributes.php (line 818)

At model->getCasts() in Hasattributes.php (line 803)

At model->hasCast('ST_ALUNO_ALU', array('date', 'datetime')) in Hasattributes.php (line 832)

At model->isDateCastable('ST_ALUNO_ALU') in Hasattributes.php (line 565)

At model->isDateAttribute('ST_ALUNO_ALU') in Hasattributes.php (line 525)

At model->setAttribute('ST_ALUNO_ALU', 'Matheus') in Model.php (line 233)

And the error messages go further still.

My table is as follows:

Schema::create('alunos', function (Blueprint $table) {
        $table->increments('ID_ALUNO_ALU');
        $table->string('ST_ALUNO_ALU', 100);
        $table->string('ST_RESPONSAVEL_ALU', 100);
        $table->integer('NM_MATRICULA_ALU');
        $table->date('DT_NASCIMENTO_ALU')->nullable();
        $table->timestamps();
    });

My form is as follows:

<form class="form" action="{{ route('alunos.store') }}" method="POST">

   <input name="_token" type="hidden" value="{{ csrf_token() }}"/>

    <div class="row">
        <div class="col-md-12">
            <label for="ST_ALUNO_ALU" class="control-label">Nome</label>
            <input type="text" class="form-control" name="ST_ALUNO_ALU" value="" autofocus>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <label for="ST_RESPONSAVEL_ALU" class="control-label">Responsável</label>
        <input type="text" class="form-control" name="ST_RESPONSAVEL_ALU" value="">
    </div>
</div>

<div class="row">
    <div class="col-md-4">
        <label for="NM_MATRICULA_ALU" class="control-label">NIS</label>
        <input type="number" class="form-control" name="NM_MATRICULA_ALU" value="">
    </div>
    <div class="col-md-4">
        <label for="DT_NASCIMENTO_ALU" class="control-label">Data de Nascimento</label>
        <input type="date" class="form-control" name="DT_NASCIMENTO_ALU" value="">
    </div>
</div>

And my controller method:

public function store(Request $request)
{
    $save = Aluno::create($request->all());

    if($save)
        redirect()->back();
    else
        throw new Exception("Não foi possível registrar o aluno");            
}

If anyone can help me, thank you, because I have no idea where the mistake is.

1 answer

1


I found the error. In my model the primary key was like this:

protected $primaryKey = ['ID_ALUNO_ALU'];

Just remove the array brackets that worked.

  • Yes, in your case should be a field for primary key, only defines with array when it’s more of a field.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.