【问题标题】:Creating default object from empty value when updating record更新记录时从空值创建默认对象
【发布时间】:2019-07-08 16:43:55
【问题描述】:

我可以在我的数据库中创建一条新记录,但是当我使用以下代码更新时,我收到错误“从空值创建默认对象”

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

如果我使用此代码,但它可以工作,但根本不处理更新到图像的链接

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

    }

【问题讨论】:

  • $gameSearch 对比。 $games。您将对象从数据库加载到$gameSearch,然后尝试填充并保存$games。将$games的所有引用更改为$gameSearch
  • 是的,谢谢
  • 马特兄弟团结起来!

标签: php laravel eloquent laravel-5.8


【解决方案1】:

$gameSearch 对比。 $games。您将对象从数据库加载到$gameSearch,然后尝试填充并保存$games。将$games的所有引用更改为$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');
        };
    };
}

【讨论】:

    猜你喜欢
    • 2020-05-28
    • 2020-04-09
    • 1970-01-01
    • 2014-08-05
    • 2015-01-28
    • 2017-11-29
    • 2017-09-30
    • 2016-08-18
    • 2018-07-18
    相关资源
    最近更新 更多