【问题标题】:Laravel Eloquent doesn't update only insertingLaravel Eloquent 不只更新插入
【发布时间】:2019-02-22 16:11:27
【问题描述】:

我在使用 PHP Laravel 更新功能时遇到了一些问题。它不更新,只插入。

 public function post_rate(){
    $o = Input::all();

    //has user already rated?!
    $query = DB::table("ratings")->select("id")->where("user", "=", Auth::user()->id)->where("story_id", "=", $o['story_id'])->get();
    foreach ( $query as $d):
        $theID = $d->id;
    endforeach;
    if ( empty($query)):  //User hasn't rated!
           $z = new Rating(); 
    else: 
          $z = new Rating($theID);  //user has rated, tell which to update  
-----------------------------^ that cause the problem!
     endif;

        $z->story_id = $o['story_id'];
        $z->user = Auth::user()->id;
        $z->rating = $o['rating'];
        $z->save();

         echo "OK";

}

当没有建立行时它可以工作,但是当我使用 new Rating(@something) 时它会失败。

“ratings”表中的列 id 是 primary 和 auto_increment。

在我的模型中我也设置了

 public static $key = 'id';

输出“$theID”还包含 mysql 行的正确 ID。

【问题讨论】:

  • 一条建议——如果你想得到一个只包含一列值的数组,你可以这样做: $idArray = DB::table("ratings")->where( "user", "=", Auth::user()->id)->where("story_id", "=", $o['story_id'])->lists('id');

标签: php laravel laravel-4


【解决方案1】:

尝试:$z = Rating::find($theID) 改为 ->first()->get()

【讨论】:

    【解决方案2】:
    $z = Rating::find($theID);
    

    $z = Rating::where('id', '=', $theID)->first();
    

    两者是等价的。

    【讨论】:

      【解决方案3】:

      要在 laravel 中更新,只需使用命令

      Rating::where('id','=',$theID)->update(array('name'=> $name));
      

      我希望这能有所帮助。

      【讨论】:

        【解决方案4】:

        老兄!

        我也遇到了同样的问题。查看源码发现设置主键名的正确字段是'primarykey'。

        例如:

        class User extends Eloquent {
            protected $table = 'users';
            protected $primaryKey = 'ID';
            public $timestamps = false;
        }
        

        【讨论】:

          【解决方案5】:

          慢慢来轻松
          看看这个,当用户没有评价你时,像以前一样保存它!

          if ( empty($query)){  //User hasn't rated!
                  $z = new Rating();    
                  $z->story_id = $o['story_id'];
                  $z->user = Auth::user()->id;
                  $z->rating = $o['rating'];
                  $z->save();
          }
          

          更新部分!执行以下操作:

           else{
              $rates = array(
                          'story_id' => $o['story_id'],
                          'user' => Auth::user()->id,
                          'rating' => $o['rating'],
                      );
          $old = Rating::find($theID);
          $old->fill($rates);
          $old->save();
          }
          

          【讨论】:

            【解决方案6】:

            对于注册,Eloquent 没有运行更新,因为我使用的方法(见下文)没有将 exists 属性设置为 true。这是因为我从请求中获取了所有返回的字段($ request-> all)并创建了一个模型,但这并不能使对象更新。

            这不起作用

            $produto = new Produto($request->all());
            $produto->update();
            

            这也没有用

            $produto = new Produto($request->all());
            
            $produtoToUpdate = Produto::find($this->id);
            $produtoToUpdate->update(get_object_vars($produto));
            

            永远不要使用 get_object_var,Eloquent 模型有虚拟属性(你用 getAttributes 获得),所以 get_object_var 没有返回你的字段。

            这项工作!!

            $produto = new Produto($request->all());
            
            if ($this->id <= 0 )
            {
                $produto->save();
            } else
            {
                $produto->exists = true;
                $produto->id = $this->id;
                $produto->update();
            }
            

            【讨论】:

              【解决方案7】:

              我也遇到了同样的问题,所以原来是雄辩的模型问题。

              首先在模型中添加一个可填充的受保护数组名称并定义属性。Like

              protected $fillable = array('story_id', 'rating');
              

              在你的情况下,有很多方法可以在 Laravel 中更新表格。

              1. $requestedRating = Rating::findOrFail($id)->first()->update($request->all()); // No need for first if the id is unique.
              
              2.  $requestedRating = Rating::findOrFail($id)->get()->update($request->all()); // fetch records from that model and update.
              
              3. $requestedRating = Rating::whereIn('id', $id)->update($request->all()); 
              

              你也可以使用填充来代替更新。

              1. $requestedRating = Rating::findOrFail($id)->first()->fill($request->all())->save();
              

              总是有 DB::table 方法,但只有在没有可用选项时才使用它。

              【讨论】:

                猜你喜欢
                • 2016-06-13
                • 2018-12-11
                • 2020-05-08
                • 2017-11-17
                • 2019-08-01
                • 1970-01-01
                • 1970-01-01
                • 2015-09-19
                • 2012-08-29
                相关资源
                最近更新 更多