Reputation: 337
I want to update record in "Events" Table but "Events" Table have no relationship with another Table
Then what should i do in Models
Here is my code and explanation
<td>
<a href="{{ route('events.edit',$event->id)}}">
<span class="glyphicon glyphicon-edit"></span>
</a>
</td>
I'm sending the $event->id to the edit.blade.php page for edit request
<form role="form" action="{{ route('events.update',$event->id)}}" method="post" enctype="multipart/form-data">
{{csrf_field()}}
{{method_field('PATCH')}}
<div class="box-body">
<div class="col-lg-6">
<div class="box-body">
<div class="col-lg-6">
<div class="form-group">
<label for="title">Event Title</label>
<input type="text" name="title" class="form-control" id="title" placeholder="Title"
value="{{$event->title}}">
</div>
<div class="form-group">
<label for="subtitle">Place</label>
<input type="text" name="place" class="form-control" id="place" placeholder="Place the Destination"
value="{{$event->place}}" >
</div>
And this form is receiving the $event->id request but when i run this code this error is occur
"Undefined variable: event (View: F:\society\resources\views\admin\events\edit.blade.php)"
Here is the Event Controller
public function edit($id)
{
$events = event::where('id',$id)->first();
return view('admin.events.edit',compact('events'));
}
And update function is here
public function update(Request $request, $id)
{
$this->validate($request,[
'title'=>'required',
'place'=>'required',
'starttime'=>'required',
'endtime'=>'required',
'datepicker'=>'required',
'body'=>'required',
'image'=>'required'
]);
if($request->hasFile('image')){
$img = $request->file('image');
$filename = time() . '.' . $img->getClientOriginalExtension();
Image::make($img)->resize(300, 300)->save( public_path('/uploads/avatars/' . $filename ) );
}
$event=event::find($id);
$event->image= $filename;
$event->title =$request->title;
$event->place =$request->place;
$event->starttime =$request->starttime;
$event->endtime =$request->endtime;
$event->datepicker =$request->datepicker;
$event->body =$request->body;
$event->save();
return redirect(route('events.index'));
}
And Model is here
use Illuminate\Database\Eloquent\Model;
class event extends Model
{
protected $table = 'events';
public function events()
{
return $this->belongsTo(event::class);
}
}
Please tell me where is the problem Problem in Model or somewhere else
Upvotes: 0
Views: 183
Reputation: 2267
Your controller code should look something like this:
public function edit($id)
{
$event = event::findOrFail($id)
return view('admin.events.edit', compact('event'));
}
You were passing the variable events
instead of event
Upvotes: 2