Call to a member function update() on null LARAVEL
20,860
{id}
not a value. You must have value for {id}
.
Example: action="/update/1"
<form action="/update/1" method="post" id="registerform" enctype="multipart/form-data">
<div class="errorpop{{ $errors->has('genre') ? ' has-error' : '' }}">
<label for="genre" class="">Genre</label>
<div class="">
@foreach($genres as $genre)
<option value="{{ $genre->genreid }}">{{ $genre->genre }}</option>
@endforeach
@if ($errors->has('genre'))
<span class="help-block">
<strong>{{ $errors->first('genre') }}</strong>
</span>
@endif
</div>
</div>
Then to update you use method where('id', $id)
replace find($id)
DB::table('games')->join('genres', 'games.genreid', '=', 'genres.genreid')->where('id', $id)->update($data);
https://laravel.com/docs/5.5/queries#updates
Related videos on Youtube
Author by
Steve Ruru
Updated on June 30, 2021Comments
-
Steve Ruru almost 3 years
I tried to update specific item but it was error 'Call to a member function update() on null'. I tried to change ->update($data) with ->insert($data) before and it works.
Controller:
public function update(Request $request, $id) { $this -> validate($request, array( 'gamename' => 'required|min:3', 'price' => 'required|int|min:1', 'genre' => 'required', 'releaseddate' => 'required|date', 'picture' => 'required|mimes:jpeg,jpg,png,gif' )); $gamename = $request->input('gamename'); $genre = $request->input('genre'); $price = $request->input('price'); $releaseddate = Carbon::parse($request->input('releaseddate')); $picture = $request->file('picture')->getClientOriginalName(); $data=array('gamename' => $gamename, 'genre'=>$genre, 'price'=>$price,'releaseddate'=>$releaseddate,'picture'=>$picture ); DB::table('games')->join('genres', 'games.genreid', '=', 'genres.genreid')->find($id)->update($data); return redirect('managegame'); }
View:
<form action="/update/{id}" method="post" id="registerform" enctype="multipart/form-data"> <div class="errorpop{{ $errors->has('genre') ? ' has-error' : '' }}"> <label for="genre" class="">Genre</label> <div class=""> @foreach($genres as $genre) <option value="{{ $genre->genreid }}">{{ $genre->genre }}</option> @endforeach @if ($errors->has('genre')) <span class="help-block"> <strong>{{ $errors->first('genre') }}</strong> </span> @endif </div> </div>
-
Steve Ruru over 6 yearsit gets the game's id which i clicked to edit || i got the same result as i use {{ $id = $_SESSION['id'] }} @AlexeyMezenin
-
Alexey Mezenin over 6 yearsSo, this gives you an object then
dd(DB::table('games')->find($id))
? and notnull
? If it does, do thisDB::table('games')->where('id', $id)->update($data)
; -
Steve Ruru over 6 yearsyes it's not
null
, i've tried your answer. it doesn't show any error but it's only refresh the edit page (it should be redirect:view) without updating the data -
Alexey Mezenin over 6 yearsSounds like validation redirects back, try to remove validation. Will the game be updated?
-
Sohel0415 over 6 yearsDB::table('games')->where('id',$id)->update($data); should work, working fine on my end, just tested
-
Steve Ruru over 6 yearssorry i've edited the HTML the name genre with genreid. it's not error now, and the page is redirected now, but the data is not updated :(
-
-
Steve Ruru over 6 yearsi've tried your answer. it doesn't show any error but it's only refresh the edit page (it should be redirect:view) without updating the data