I can create a new record in my DB fine but when I update using the below code I get error "Creating default object from empty value"
public function update(Request $request, $id)
{
$gameSearch = Game::findOrFail($id);
if($file = $request->file('image')){
$name = $file->getClientOriginalName();
if($file->move('images/games/', $name)){
$games->image = 'images/games/' . $name;
$games->title = $request->title;
$games->price = $request->price;
$games->category_id = $request->category_id;
$games->promote = $request->promote;
$games->sold = 0;
$games->save();
return redirect()->route('admin.games');
};
};
}
Route:
Route::resource('/admin/games', 'AdminGamesController', [
'names'=>[
'index'=>'admin.games.index',
'create'=>'admin.games.create',
'store'=>'admin.games.store',
'edit'=>'admin.games.edit',
'show'=>'admin.games.show',
'destroy'=>'admin.games.destroy',
]]);
Form:
{!! Form::model($gameSearch, ['method' =>'PATCH', 'action'=> ['AdminGamesController@update', $gameSearch->id], 'files'=>true, 'enctype'=>'multipart/form-data']) !!}
<div class="form-group {{$errors->has('title') ? 'has-error' : ''}}">
{!! Form::label('title', 'Game Title:') !!}
{!! Form::text('title', null, ['class'=>'form-control', 'rows' => 3])!!}
@if($errors->has('title'))
{{$errors->first('title')}}
@endif
</div>
<div class="form-group {{$errors->has('image') ? 'has-error' : ''}}">
{!! Form::label('image', 'Image:') !!}
{!! Form::file('image', null, ['class'=>'form-control'])!!}
@if($errors->has('image'))
{{$errors->first('image')}}
@endif
</div>
If I use this code however it works but doesn't handle updating the link to the image at all
public function update(GamesRequest $request, $id)
{
$gameSearch = Game::findOrFail($id);
$input = $request->all();
if($file = $request->file('image')){
$name = $file->getClientOriginalName();
$file->move('images/games/', $name);
}
$gameSearch->update($input);
return redirect('admin/games');
}