4

我在使用 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。

4

7 回答 7

3

尝试:$z = Rating::find($theID)使用->first()->get()代替

于 2013-06-17T18:25:23.747 回答
2
$z = Rating::find($theID);

或者

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

两者是等价的。

于 2013-06-17T19:56:58.940 回答
2

老兄!

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

例如:

class User extends Eloquent {
    protected $table = 'users';
    protected $primaryKey = 'ID';
    public $timestamps = false;
}
于 2013-09-27T01:00:11.050 回答
2

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

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

我希望这能有所帮助。

于 2013-06-18T11:01:02.147 回答
0

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

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();
}
于 2013-09-29T22:25:59.667 回答
0

我也面临同样的问题,所以它原来是雄辩的模型问题。

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

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 方法,但只有在没有可用选项时才使用它。

于 2019-02-22T08:11:27.147 回答
0

对于注册,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();
}
于 2017-06-16T16:08:08.650 回答