douzhanjia0773 2019-07-08 16:43
浏览 117

更新记录时从空值创建默认对象

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

    }
  • 写回答

1条回答 默认 最新

  • dongxian0421 2019-07-08 17:13
    关注

    $gameSearch Vs. $games. You load your object from the DB into $gameSearch but then, try to populate and save $games. Change all references of $games to $gameSearch.

    public function update(Request $request, $id)
    {
        $gameSearch = Game::findOrFail($id);
    
        if ($file = $request->file('image')) {
            $name = $file->getClientOriginalName();
            if ($file->move('images/games/', $name)) {
                $gameSearch->image = 'images/games/' . $name;
                $gameSearch->title = $request->title;
                $gameSearch->price = $request->price;
                $gameSearch->category_id = $request->category_id;
                $gameSearch->promote = $request->promote;
                $gameSearch->sold = 0;
                $gameSearch->save();
    
                return redirect()->route('admin.games');
            };
        };
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100