Select with Laravel foreign key

Asked

Viewed 923 times

2

Hello, I managed to solve a foreign key problem just now and came across a second, I have no idea how to recover in a select in the view the data from another table. If anyone could give me a simple example, I’d appreciate it. 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’s 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 my php Edit.blade.

@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.

1 answer

1


You need to first 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 model Course from within Pupil, it is also important to realize the opposite, that is to say in Course that those data belong to the student:

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

The methods of Eloquent ORM for this are the hasOne(), hasMany(), belongsTo() and belongsToMany().

With properly configured relationship it is simple to recover information:

<?php

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

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

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

  • Thanks, Giovanni, it was very helpful, now I can recover all the data :)

Browser other questions tagged

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