在null LARAVEL上调用成员函数update()

时间:2018-01-07 16:25:01

标签: php mysql database laravel

我尝试更新特定项目但是错误'调用成员函数update()on null'。 我之前尝试使用 - > insert($ data)更改 - >更新($ data)并且它有效。

控制器:

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');
    }

查看:

<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>

4 个答案:

答案 0 :(得分:1)

userSignupPostRequest不是值。您必须拥有{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> 替换where('id', $id)

find($id)

https://laravel.com/docs/5.5/queries#updates

答案 1 :(得分:0)

确保你有对象

$game = DB::table('games')->
join('genres', 'games.genreid', '=', 'genres.genreid')->where('id',$id)->get();

//确保你有所需的对象。然后

  $game->update($data);

答案 2 :(得分:0)

我也不确定这条线是否符合预期

DB::table('games')->join('genres', 'games.genreid', '=', 'genres.genreid')->find($id)->update($data);

你想要更新什么? gamesgenres?你不能像这样一次更新2个表。您需要找到要更新的games行,更新该行以及连接的表行,并更新它,不能一次完成所有操作。

但是您尝试从一个简单的表单更新2个表格的事实,在同一方法中表明您的表格没有正确布局?

答案 3 :(得分:-1)

id)->update($data);

    return redirect('managegame');
}

查看:

有('流派')? '有错误':''}}"> 类型
        <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>