Select with foreign key Laravel

1
Hello, I have been able to solve a foreign key problem now and I came across a second, I have no idea how to retrieve in a select in the view the data of another table. If anyone can give me a simple example, I would be grateful. My create, store, and edit from my controller

public function create()
    {

        return view('curso.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $cursos = new Curso([
            'nome' => $request->get('nome')
        ]);

        $cursos->save();
        return redirect('/cursos');
    }

public function edit($id)
    {
        $cursos = Curso::find($id);

        return view('curso.edit', compact('cursos','id'));
    }

And here my view create.blade.php

@extends('master')
@section('content')
<div class="container">
  <h2>Cadastrar Curso</h2>
  <form method="post" action="{{action('CursoController@store')}}">

    <div class="form-row">
      {{csrf_field()}}
      <div class="form-group col-md-12">
        <label for="lgFormGroupInput">Nome</label>
        <input type="text" class="form-control" id="lgFormGroupInput" placeholder="Nome do Curso" name="nome">
      </div>
    </div>

    <div class="form-row">
      <div class="col-md-5"></div>
      <input type="submit" class="btn btn-primary">
      <a class="btn btn-danger" href="{{ action('CursoController@index') }}"> Cancelar</a>

    </div>


  </form>
</div>
@endsection

here is my edit.blade.php

@extends('master')
@section('content')
<div class="container">
  <h2>Editar Curso</h2>
  <form method="post" action="{{action('CursoController@update', $id)}}">
    <div class="form-group row">
      {{csrf_field()}}
       <input name="_method" type="hidden" value="PATCH">
      <label for="lgFormGroupInput" class="col-sm-2 col-form-label col-form-label-lg">Nome</label>
      <div class="col-sm-10">
        <input type="text" class="form-control form-control-lg" id="lgFormGroupInput" placeholder="Nome" name="nome" value="{{$cursos->nome}}">
      </div>
    </div>

    <div class="form-group row">
    <label for="lgFormGroupInput" class="col-sm-2 col-form-label col-form-label-lg">Professor</label>
      <div class="col-sm-10">
        <input type="text" class="form-control form-control-lg" id="lgFormGroupInput" placeholder="Nome" name="nome" value="{{$cursos->professor->nome}}">
      </div>
    </div>


    <div class="form-group row">
      <div class="col-md-2"></div>
      <button type="submit" class="btn btn-primary">Editar</button>
      <a class="btn btn-danger" href="{{ action('CursoController@index') }}"> Cancelar</a>
    </div>
  </form>
</div>
@endsection

I need to change this teacher to a select.

    
asked by anonymous 26.03.2018 / 00:50

1 answer

1

You need to define the relationship in the model, for example:

<?php
    class Aluno extends Eloquent {
    // ...
    public function cursos(){
        return $this->hasMany('Curso');
    }
?>

This allows you to access the Course template from within Student , it is also important to do the opposite, that is, say in Course that those data belongs to student:

<?php
    class Curso extends Eloquent {
    // ...
    public function alunos(){
        return $this->belongsTo('Aluno');
    }
?>
The Eloquent ORM methods for this are hasOne (), hasMany (), belongsTo (), > and belongsToMany () .

With the relationship set up correctly it is easy to retrieve the information:

<?php

    $jose = Aluno::where('name', '=', 'José da Silva')->first();

    foreach ($jose->cursos as $curso){
        //...
    }
?>

For more information, see the Eloquent ORM documentation on the Laravel page.

    
26.03.2018 / 13:44