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.
Thanks, Giovanni, it was very helpful, now I can recover all the data :)
– Linton Junior