Sep 4, 2018
0
Level 1
How to use Form Model Binding with related Data and Image on Edit Form
I am still at my Upcoming Event Function. i have now got an issue with the Edit Form. I use Form Model Binding, hope this is how it is called. It works well on my Events Table, pulls in the Data and displays it in my Form, but I do not get my related data. i thought to add the with('eventdates') but the dates are not added. I allow to add multiple Dates with a dynamic created input field so I can not even say if 1 or multiple fields should be created. The image also needs to be displayed of course. Is this actually possible to do with Form Model Binding or rather not?
EventController:
public function edit($id)
{
$event = Event::findOrFail($id);
return view('memberarea.events.edit', compact('event'));
}
class Event extends Model {
protected $fillable = ['event_name', 'event_description'];
public function eventdates()
{
return $this->hasMany('App\EventDates');
}
}
class EventDates extends Model
{
protected $fillable = [ 'event_date','event_starttime','event_endtime','event_id'];
protected $dates = ['event_date'];
public function event()
{
return $this->belongsTo('App\Event', 'event_id');
}
}
class Image extends Model
{
public $fillable = ['image_name'];
public function events()
{
return $this->hasOne('App\Event','event_image_id');
}
}
My edit form looks like this:
{!! Form::model($event, ['method' => 'PATCH', 'action' => ['EventController@update',$event->id]]) !!}
<div class="form-group row">
{{ Form::label('eventname', 'Event Name:',array('class'=>'required col-sm-2 col-form-label')) }}
<div class="col-sm-4">
{{ Form::text('event_name', null, array('class' => 'form-control')) }}
@if ($errors->has('event_name'))
<span class="text-danger">{{ $errors->first('event_name') }}</span>
@endif
</div>
</div>
<div class="dates">
<div class="form-group row">
{{ Form::label('Date/Time:', '',array('class'=>'required col-sm-2 col-form-label')) }}
{{ Form::text('event_date[]', null, array('id' => '', 'class' => 'eventDate form-control','placeholder' => 'Date')) }}
<div class="input-group-append">
<span class="input-group-text"><i class="fa fa-calendar" onclick="$('#eventDate').focus();"></i></span>
</div>
@if ($errors->has('event_date'))
<span class="text-danger">{{ $errors->first('event_date') }}</span>
@endif
</div>
</div>
<input type="file" class="form-control-file" name="event_image" id="exampleInputFile" aria-describedby="fileHelp">
<small id="fileHelp" class="form-text text-muted">Please upload a valid image file. Size of image should not be more than 2MB.</small>
{{ Form::submit('Save Details', array('class' => 'btn btn-success btn-lg btn-block')) }}
{{ Form::close() }}
Please or to participate in this conversation.