在 null LARAVEL 上调用成员函数 update()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48139158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call to a member function update() on null LARAVEL
提问by Steve Ruru
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.
我试图更新特定项目,但它是错误“调用成员函数 update() on null”。我之前尝试使用 ->insert($data) 更改 ->update($data) 并且它有效。
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>
回答by Joan Nguyen
{id}
not a value. You must have value for {id}
.
Example: action="/update/1"
{id}
不是一个值。你必须对{id}
. 例子: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)
然后更新你使用方法where('id', $id)
替换find($id)
DB::table('games')->join('genres', 'games.genreid', '=', 'genres.genreid')->where('id', $id)->update($data);
回答by Mahdi Younesi
Make sure you got the object
确保你得到了对象
$game = DB::table('games')->
join('genres', 'games.genreid', '=', 'genres.genreid')->where('id',$id)->get();
//make sure you have the desired object.then
//确保你有想要的对象。然后
$game->update($data);
回答by John Halsey
I'm nor sure this line works as you expect
我不确定这条线是否如你所愿
DB::table('games')->join('genres', 'games.genreid', '=', 'genres.genreid')->find($id)->update($data);
DB::table('games')->join('genres', 'games.genreid', '=', 'genres.genreid')->find($id)->update($data);
What are you trying to update? games
or genres
? You can't update 2 tables at once like that. You'd need to find the games
row to update, update that that, and the joined table row, and update that, cannot do it all at once.
你想更新什么?games
或者genres
?您不能像那样一次更新 2 个表。您需要找到games
要更新的行,更新该行,以及连接的表行并更新该行,不能一次全部完成。
But the fact that you're trying to update 2 tables from one simple form, in the same method suggests that your tables is not properly laid out?
但是,您尝试以相同的方法从一个简单的表单更新 2 个表的事实表明您的表布局不正确?