0
Expensive,
I have a very boring and persistent mistake.
Undefined variable: institutions (View:
I have searched several times and do not know why the data is not passed to the form.
Controller
namespace App Http Controllers;
use Illuminate Http Request; use App Http Controllers Controller; use App Http Requests Occurrencesrequest; use Carbon Carbon;
use App Occurrence;
class Occurrencescontroller extends Controller { //public Function __Construct() //{
//}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
//variavel #ocorrencias amarzena o comteudo do model/bd (Ocorrencia) e realiza a paginação
$ocorrencias = Ocorrencia::paginate(5);
//$ocorrencias = Ocorrencia::all();
//Compact na variavel ocorrencias (Array)
return view('ocorrencias.index', compact('ocorrencias'));
//dd(hora_final, hora_inicial)
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
//retorna a função create na view
return view('ocorrencias.create');
//return view('ocorrencias.create')->with('institutions',Institution::lists('name','id'));
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return Response
*/
//
public function store(OcorrenciaRequest $request)
{
$ocorrencia = Ocorrencia::create($request->all());
$ocorrencia = $this->setOcorrenciaRelations($ocorrencia, $request);
$ocorrencia->save();
return redirect()->route('ocorrencia.index');
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show(Ocorrencia $ocorrencia)
{
return view('ocorrencias.show', compact('ocorrencia'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit(Ocorrencia $ocorrencia)
{
//Compact na variavel ocorrencia.
return view('ocorrencias.edit', compact('ocorrencia'));
}
/**
* Update the specified resource in storage.
*
* @param Request $request
* @param int $id
* @return Response
*/
public function update(OcorrenciaRequest $request, Ocorrencia $ocorrencia)
{
$ocorrencia->update($request->all());
$ocorrencia = $this->setOcorrenciaRelations($ocorrencia, $request);
$ocorrencia->save();
return redirect()->route('ocorrencia.index');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy(Ocorrencia $ocorrencia)
{
$ocorrencia->delete();
return redirect()->route('ocorrencia.index');
}
private function setOcorrenciaRelations(Ocorrencia $ocorrencia, Request $request)
{
$institution = Institution::findOrFail($request->institution_id);
$ocorrencia->institution()->associate($institution);
$aplication = Aplication::findOrFail($request->aplication_id);
$ocorrencia->aplication()->associate($aplication);
$ocorrencia->causes()->sync($request->causes_list);
//$user = User::findOrFail($request->user_id);
//$ocorrencia->user()->associate($user);
return $ocorrencia;
}
}
Form
{!! Form::label('initialdata_, 'initial date') !! } {!! Form::date('initial data_null, ['class' => 'form-control', 'placeholder' => 'Initial Date']) !! } {!! Form::label('initial date', 'Initial time') !! } {!! Form::time('initial data_null, ['class' => 'form-control', 'placeholder' => 'Initial Time']) !! } {!! Form::label('data_final', 'Final Date') !! } {!! Form::date('data_final', null, ['class' => 'form-control', 'placeholder' => 'Final Date']) !! } {!! Form::label('hora_final', 'Final Hour') !! } {!! Form::time('hora_final', null, ['class' => 'form-control', 'placeholder' => 'Final Time']) !! } {!! Form::label('institution_id', 'Institution') !! } {!! Form::select('institution_id', $institutions, null, ['class' => 'form-control', 'placeholder' => 'Select an Institution...']) !! } {!! Form::label('logged in', 'logged in') !! } {!! Form::text('logged', null, ['class' => 'form-control', 'placeholder' => 'Logged in']) !! } {!! Form::label('institution_id', 'Institution') !! } {!! Form::select('institution_id', $institutions, null, ['class' => 'form-control', 'placeholder' => 'Select an Institution...']) !! } {!! Form::label('observation', 'occurrence') !! } {!! Form::textarea('observation', null, ['class' => 'form-control', 'placeholder' => 'Occurrence...']) !!}{!! Form::Ubmit('Send', ['class' => 'btn btn-default']) !!}
Model
namespace App;
use Illuminate Database Eloquent Model;
class Occurrence extends Model {
//protected $table = "occurrences";
protected $fillable = [
'data_inicial',
'hora_inicial',
'data_final',
'hora_final',
'logados',
'observacao'
];
public function institution()
{
return $this->belongsTo('App\Institution');
}
public function causes()
{
return $this->belongsToMany('App\Cause');
}
public function aplication()
{
return $this->belongsTo('App\Aplication');
}
/*public function user()
{
return $this->belongsToMany('App\User');
}*/
// Eloquent Accessor
// Cria uma nova propriedade dinamica
// Convenção: get + CampoEmCamelCase + Attribute
// Será convertido em $this->campo_em_camel_case
public function getCausesListAttribute()
{
return $this->causes->lists('id')->toArray();
}
}
Can you explain more of your code? which controller method are you trying to use? From what I’ve seen, you’re not passing this variable to the view!
– PV Telles
I am trying to use create <code>public Function create() { //returns the create function in view Return view('occurrences.create'); } </code>
– Ronaldo Adriano
then, you are not passing the variable to the route, you must pass the variable $institutions to your view.
– PV Telles