Laravel Eloquent不更新,只插入数据。

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";

}

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

"ratings"表中的id列是主键和自动递增的。

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

 public static $key = 'id';

输出"$theID"还包含了mysql行的正确ID。


一条建议 - 如果你想获取一个仅包含一个列中值的数组,可以这样做: $idArray = DB::table("ratings")->where("user", "=", Auth::user()->id)->where("story_id", "=", $o['story_id'])->lists('id'); - misaizdaleka
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

在注册时,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();
}

0

我也遇到了同样的问题,结果发现是 eloquent 模型的问题。

首先,在你的模型中添加一个受保护的数组名 fillable,并定义属性。例如:

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

你可以使用 fill 代替 update。

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

始终存在DB :: table方法,但只在没有其他选项时使用。


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接