Auto-increment not working in Laravel

Asked

Viewed 27 times

0

I’m creating a note control system, which basically contains 3 tables at the moment: Teachers, Materials and Activities. I am at the beginning of the project, in which I only made the teacher registration view.

inserir a descrição da imagem aqui

However, when entering the record, the ID (which is hidden in the html) is not receiving value. It does not return any error. When giving Confirm, only a refresh occurs on the screen. I looked for tips on some videos and forums but nothing that helped. I’m using Pgadmin. Below is the Migration code, controller and routes.

HTML

@extends('cabecalho')

@extends('form')

@section('cont2')
    <form action="{{url('professor')}}" method="post">
        @csrf
        <input type="hidden" name="idprofessor">
        <label for="">Nome</label>
        <br>
        <input type="text" name="nome" id="nome" placeholder="Nome do professor">
        <br><br>
        <label for="nome">Sobrenome</label>
        <br>
        <input type="text" name="sobrenome" id="sobrenome" placeholder="Sobrenome do professor">
        <br><br>
        <label for="materia">Materia</label>
        <br>
        <input type="text" name="materia" id="materia" placeholder="Matéria">
        <br><br>
        <label for="numero">Número</label>
        <br>
        <input type="text" name="numero" id="numero" placeholder="(xx) x xxxx-xxxx" maxlength="12">
        <br><br>
        <label for="observacao">Observação</label>
        <br>
        <textarea name="observacao" id="observacao" cols="30" rows="10"></textarea>
        <br>
        <input type="submit" class="submit" value="Confirmar">
    </form>
@stop

Migration

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProfessoresTable extends Migration
{
    public function up()
    {
        Schema::create('professores', function (Blueprint $table) {
            $table->increments('id');
            $table->string('nome',20);
            $table->string('sobrenome',50);
            $table->foreignId('materia_id',30);
            $table->string('numero',11);
            $table->longText('observacao');
        });
    }    
    public function down()
    {
        Schema::dropIfExists('professores');
    }}

Controller

<?php

namespace App\Http\Controllers;

use App\Professor;
use Illuminate\Http\Request;
use Symfony\Component\Console\Input\Input;
use Illuminate\Database\Eloquent\Model;


class ProfessorController extends Controller
{
    public function create()
    {
        return view('professor.cadastro-professor');
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'idprofessor' => 'required',
            'nome'        => 'required',
            'sobrenome'   => 'required',
            'materia'     => 'required',
            'telefone'    => 'required'
        ]);

        $professor = new Professor([
            'idprofessor' => $request->get('idprofessor'),
            'nome'        => $request->get('nome'),
            'sobrenome'   => $request->get('sobrenome'),
            'materia'     => $request->get('materia'),
            'telefone'    => $request->get('telefone')
        ]);

        $professor->save();

        return view('professor.cadastro-professor');
    }

Routes

<?php

use Illuminate\Support\Facades\Route;
Route::get('/', function() {
    return view('inicio');
});

Route::resource('Professor', 'ProfessorController');
Route::get('/cadastros', 'Controller@list');
Route::get('/cadastros/cadastro-professor', 'ProfessorController@create');
Route::post('/professor', 'ProfessorController@store');

In short, my problem is with the ID that is not increasing. I thank you, and I also apologize if it is something gross.

  • Confirm that the request received in the controller has value, with dd($request->get('idprofessor')). If yes, the problem must be in the teacher’s creation, where you assign the value of the $request->idprofessor to idprofessor, and in the database the field is called id

2 answers

0

You can also insert with mass assignment.

View

@extends('cabecalho')

@extends('form')

@section('cont2')
    <form action="{{url('professor')}}" method="post">
        @csrf
        <label for="">Nome</label>
        <br>
        <input type="text" name="nome" id="nome" placeholder="Nome do professor">
        <br><br>
        <label for="nome">Sobrenome</label>
        <br>
        <input type="text" name="sobrenome" id="sobrenome" placeholder="Sobrenome do professor">
        <br><br>
        <label for="materia">Materia</label>
        <br>
        <select name="materia_id">
            <option value='1'>Materia A</option>
            <option value='2'>Materia B</option>
            <option value='3'>Materia C</option>
            ...
        </select>
        <br><br>
        <label for="numero">Número</label>
        <br>
        <input type="text" name="numero" id="numero" placeholder="(xx) x xxxx-xxxx" maxlength="12">
        <br><br>
        <label for="observacao">Observação</label>
        <br>
        <textarea name="observacao" id="observacao" cols="30" rows="10"></textarea>
        <br>
        <input type="submit" class="submit" value="Confirmar">
    </form>
@stop
## Cabeçalhos ##

Controller

$this->validate($request, [
            'nome'        => 'required',
            'sobrenome'   => 'required',
            'materia_id'  => 'required',
            'numero'      => 'required'
        ]);
$professor = Professor::create($request->all());

if($professor){
    return redirect()->route('nome_rota');
}

Model Professor

protected $fillable = ['nome', 'sobrenome', 'materia_id', 'numero', 'observacao'];

Remember that you must have matters registered at the bank to bring them to your view, and then save as Foreign Key.

0

The error is in the process of sending the idprofessor on the object, because if you want autoincrement, you should not load the attribute on the object in the controller layer, considering the store method.

How the controller should look:

Controller

<?php

namespace App\Http\Controllers;

use App\Professor;
use Illuminate\Http\Request;
use Symfony\Component\Console\Input\Input;
use Illuminate\Database\Eloquent\Model;

class ProfessorController extends Controller
{

    public function create()
    {
        return view('professor.cadastro-professor');
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'nome'        => 'required',
            'sobrenome'   => 'required',
            'materia'     => 'required',
            'telefone'    => 'required'
        ]);

        $professor = new Professor([
            'nome'        => $request->get('nome'),
            'sobrenome'   => $request->get('sobrenome'),
            'materia'     => $request->get('materia'),
            'telefone'    => $request->get('telefone')
        ]);

        $professor->save();

        return view('professor.cadastro-professor');
    }
}

Browser other questions tagged

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