Make a relationship with data already created

Asked

Viewed 21 times

-1

I’m working on a school registration system with Alavel. It currently has two tables in the database: the list of Classes and the list of Teachers. When registering a new class, I want to be able to insert a registered teacher, but every time I try this, Aravel tries to create a new teacher.

Migration das Turmas:

Schema::create('turmas', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('turma');
            $table->string('nivel');
            $table->string('ano');
            $table->string('turno');
            $table->integer('vagas');
            $table->bigInteger('professor')->unsigned()
                ->references('id')->on('professores')
                ->onDelete('cascade');
            $table->timestamps();
        });

Class Model

protected $table = 'turmas';
    
    protected $fillable = [
        'turma',
        'nivel',
        'ano',
        'turno',
        'vagas',
        'professor'
    ];

    public function professor() {
        return $this->hasOne('App\Professor', 'id');
    }

Class Controller

public function store(Request $request)
    {
        $class = new Turma;
        $class->turma = $request->input('turma');
        $class->nivel = $request->input('nivel');
        $class->ano = $request->input('ano');
        $class->turno = $request->input('turno');
        $class->vagas = $request->input('vagas');
        $class->professor()->professor = $request->input('professor');
        $class->save();
    }

Migration of Teachers

 Schema::create('professores', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('nome');
            $table->string('sobrenome');
            $table->string('sexo');
            $table->string('data_Nascimento');
            $table->string('tel')->nullable();
            $table->string('tel2')->nullable();
            $table->string('email')->unique();
            $table->timestamps();
        });

Professor’s Model

protected $table = 'professores';

    protected $fillable = [
        'nome',
        'sobrenome',
        'sexo',
        'data_Nascimento',
        'tel',
        'tel2',
        'email'
    ];
    
    public function turma()
    {
        return $this->belongsTo('App\Turma');
    }

Controller of the Professor

public function store(Request $request)
    {
        $prof = new Professor();
        $prof->nome = $request->input('nome');
        $prof->sobrenome = $request->input('sobrenome');
        $prof->sexo = $request->input('sexo');
        $prof->data_Nascimento = $request->input('data_Nascimento');
        $prof->tel = $request->input('tel');
        $prof->tel2 = $request->input('tel2');
        $prof->email = $request->input('email');
        $prof->save();
        
        return redirect()->route('listaProfessor');
    }

Registration form

<div class="container col-8" align="center">
            <input type="hidden" id="id" class="form-control">

                {{--- Formulario Turma ---}}
                <div class="form-group col-md-6">
                    <label for="turma" class="control-label">Turma: *</label>
                    <div class="input-group">
                        <input type="text" class="form-control" id="turma" name="turma" placeholder="Nome da Turma"
                            value="{{ isset($cras->turma) ? $cras->turma : old('turma') }}">
                    </div>
                </div>

                {{--- Formulário Nível ---}}

                <div class="form-group col-md-6">
                    <label for="sobrenome" class="control-label">Nível: *</label>
                    <div class="input-group">
                        <select name="nivel" id="nivel" class="form-control">
                            <option value="" selected disabled>Selecione uma Opção</option>
                            <option value="Fundamental">Fundamental</option>
                            <option value="Medio">Médio</option>
                        </select>
                    </div>
                </div>

                {{--- Formulário Ano ---}}

                <div class="form-group col-md-6">
                    <label for="ano" class="control-label">Ano: *</label>
                    <div class="input-group">
                        <select name="ano" id="ano" class="form-control">
                            <option value="" selected disabled>Selecione um Ano</option>
                            <option data-ano="Fundamental" value="1° ano">1º Ano</option>
                            <option data-ano="Fundamental" value="2° ano">2º Ano</option>
                            <option data-ano="Fundamental" value="3° ano">3º Ano</option>
                            <option data-ano="Fundamental" value="4° ano">4º Ano</option>
                            <option data-ano="Fundamental" value="5° ano">5º Ano</option>
                            <option data-ano="Fundamental" value="6° ano">6º Ano</option>
                            <option data-ano="Fundamental" value="7° ano">7º Ano</option>
                            <option data-ano="Fundamental" value="8° ano">8º Ano</option>
                            <option data-ano="Fundamental" value="9° ano">9º Ano</option>
                            <option data-ano="Medio" value="1° ano">1º Ano</option>
                            <option data-ano="Medio" value="2° ano">2º Ano</option>
                            <option data-ano="Medio" value="3° ano">3º Ano</option>
                        </select>
                    </div>
                </div>

                {{--- Formulário Turno ---}}

                <div class="form-group col-md-6">
                    <label for="data_Nascimento" class="control-label">Turno: *</label>
                    <div class="input-group">
                        <select name="turno" id="turno" class="form-control">
                            <option value="" selected disabled>Selecione um turno</option>
                            <option value="Manhã">Manhã</option>
                            <option value="Tarde">Tarde</option>
                            <option value="Noite">Noite</option>
                        </select>
                    </div>
                </div>

            {{--- Formulario Vagas ---}}
                <div class="form-group col-md-6">
                    <label for="vagas" class="control-label">Número de Vagas: *</label>
                    <div class="input-group ">
                        <input type="text" class="form-control phone_with_ddd" name="vagas" id="vagas"
                            value="{{ isset($cras->vagas) ? $cras->vagas : old('vagas') }}"
                            placeholder="ex: 10" />
                    </div>
                </div>

                {{--- Formulario Professor ---}}

                <div class="form-group col-md-6">
                    <label for="tel2" class="control-label">Professor:</label>
                    <div class="input-group">
                        <select name="professor" id="professor" class="form-control">
                            <option value="" selected disabled>Selecione um Professor</option>
                            @foreach ($professor as $prof)
                                <option value="{{ $prof->id }}">{{ $prof->nome }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
            
        <div class="card-footer">
            <button type="submit" class="btn btn-primary">Salvar</button>
            <a class="btn btn-secondary" href="{{ url()->previous() }}">Cancelar</a>
        </div>
    </div>

1 answer

0

In the class controller, on this line:

$class->professor()->professor = $request->input('professor');

You are trying to add an entire value to a field that does not exist in Professor, when you use $class->professor() the "return" would be a model relating. Just associate the if you are receiving in the request directly to the teacher fields of the class model

$class->professor = $request->input('professor');

Browser other questions tagged

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