mardi 3 novembre 2015

{!! Form::model not working in laravel 5

I'm trying to fill my form when user Clicks Edit Thing is my form is not filled. I've checked that the $tournament variable passed to the view contains right information. I could just set default value to $tournament->field, but I saw in laracast that it is not necesary, there is a simpler way to do it.

Here is my controller class TournamentController extends Controller {

public function edit($id)
{
    $places = Place::lists('name', 'id');
    $tournament = Tournament::findOrFail($id);
    return view('tournaments.edit', compact('tournament', 'places'));
}

Model

class Tournament extends Model {

protected $table = 'Tournament';
public $timestamps = true;

protected $fillable = [
    'name',
    'tournamentDate',
    'registerDateLimit',
    'placeId'

];

protected $dates = ['tournamentDate','registerDateLimit'];

public function geTournamentDateAttribute($date)
{
    return $date == "0000-00-00 00:00:00" ? "0000-00-00 00:00:00" : $date;
}

public function getLimitRegisterDateAttribute($date)
{
    return $date == "0000-00-00 00:00:00" ? "0000-00-00 00:00:00" : $date;
}
public function setTournamentDateAttribute($date){
    $this->attributes['tournamentDate'] = Carbon::createFromFormat('Y-m-d', $date);
}
public function setLimitRegisterDateAttribute($date){
    $this->attributes['registerDateLimit'] = Carbon::createFromFormat('Y-m-d', $date);
}

}

View

edit.blade.php

@extends('app')

@section('content')

<h1>@lang('crud.editModel', ['currentModelName' => $currentModelName])</h1>
<hr/>
{!! Form::model($tournament, ['method'=>"PATCH", "action" => ["TournamentController@update", $tournament->id]]) !!}

@include("tournaments.form", ["submitButton" => "@lang('crud.updateModel', ['currentModelName' => $currentModelName])"])


{!! Form::close()!!}

@include("errors.list")
@stop

form.blade.php

<div class="form-group">
{!!  Form::label('name', trans('crud.name')) !!}
{!!  Form::text('name', '', ['class' => 'form-control']) !!}
</div>

<div class="form-group">
{!!  Form::label('tournamentDate', trans('crud.eventDate')) !!}
{!!  Form::input('date', 'tournamentDate', \Carbon\Carbon::now()->format('Y-m-d'), ['class' => 'form-control']) !!}
</div>

<div class="form-group">
{!!  Form::label('limitRegistrationDate', trans('crud.fullLimitDateRegistration')) !!}
{!!  Form::input('date', 'limitRegistrationDate', \Carbon\Carbon::now()->format('Y-m-d'), ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!!  Form::label('placeId', trans('crud.place')) !!}
{!!  Form::select('placeId', $places,null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!!  Form::submit($submitButton, ['class' => 'btn btn-primary form-control']) !!}
</div>

Any idea???



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire