Caros,
Estou com um erro bem chato e persistente.
Undefined variable: institutions (View:
Já procurei varias vezes e não sei o porque os dados não são passados para o form.
Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Http\Requests\OcorrenciaRequest; use Carbon\Carbon;
use App\Ocorrencia;
class OcorrenciasController 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('data_inicial', 'Data Inicial') !!} {!! Form::date('data_inicial', null, ['class' => 'form-control', 'placeholder' => 'Data Inicial']) !!} {!! Form::label('data_inicial', 'Hora Inicial') !!} {!! Form::time('data_inicial', null, ['class' => 'form-control', 'placeholder' => 'Hora Inicial']) !!} {!! Form::label('data_final', 'Data Final') !!} {!! Form::date('data_final', null, ['class' => 'form-control', 'placeholder' => 'Data Final']) !!} {!! Form::label('hora_final', 'Hora Final') !!} {!! Form::time('hora_final', null, ['class' => 'form-control', 'placeholder' => 'Hora Final']) !!} {!! Form::label('institution_id', 'Instituição') !!} {!! Form::select('institution_id', $institutions, null, ['class' => 'form-control', 'placeholder' => 'Selecione uma Instituição...']) !!} {!! Form::label('logados', 'Logados') !!} {!! Form::text('logados', null, ['class' => 'form-control', 'placeholder' => 'Logados']) !!} {!! Form::label('institution_id', 'Instituição') !!} {!! Form::select('institution_id', $institutions, null, ['class' => 'form-control', 'placeholder' => 'Selecione uma Instituição...']) !!} {!! Form::label('observacao', 'Ocorrência') !!} {!! Form::textarea('observacao', null, ['class' => 'form-control', 'placeholder' => 'Ocorrência...']) !!}{!! Form::submit('Enviar', ['class' => 'btn btn-default']) !!}
Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Ocorrencia extends Model {
//protected $table = "ocorrencias";
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(); }
}