Laravel Eloquent不会仅更新插入

问题描述 投票:4回答:7

我正面临着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";

}

当没有成立行时它会工作,但是当我使用新的等级(@something)时,它会失败。

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

在我的模型中,我也设置了

 public static $key = 'id';

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

php laravel laravel-4
7个回答
3
投票

尝试:$z = Rating::find($theID)->first()->get()相反


2
投票
$z = Rating::find($theID);

要么

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

两者都是等价的。


2
投票

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

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

我希望这可以提供一些帮助。


2
投票

老兄!

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

例如:

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

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

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 Model有虚拟属性(你得到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();
}

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

© www.soinside.com 2019 - 2024. All rights reserved.