简体   繁体   English

Laravel Eloquent不会仅更新插入

[英]Laravel Eloquent doesn't update only inserting

I am facing some problems with PHP Laravel update function. 我正面临着PHP Laravel更新功能的一些问题。 It doesn't update, only insert. 它不会更新,只会插入。

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

}

It works when no rows founded, but when i will use new Rating(@something) it fails. 当没有成立行时它会起作用,但是当我使用新的等级(@something)时,它会失败。

the column id in "ratings" table is primary and auto_increment. “ratings”表中的列id是primary和auto_increment。

In my model i have also set up 在我的模型中,我也设置了

 public static $key = 'id';

The output "$theID" also contains the correctly ID for the mysql row. 输出“$ theID”还包含mysql行的正确ID。

尝试: $z = Rating::find($theID) ,而不是->first()->get()

$z = Rating::find($theID);

or 要么

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

Both are equivalent. 两者都是等价的。

In order to update in laravel simply use the command 要在laravel中更新,只需使用该命令即可

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

I hope this can be of some help. 我希望这可以提供一些帮助。

The dude ! 老兄!

I was taking the same problem. 我也遇到了同样的问题。 Checking out the source code, I found that the correct field to set the primary key name is 'primarykey'. 检查源代码,我发现设置主键名称的正确字段是'primarykey'。

For example: 例如:

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

Take it easy ! 别紧张
take a look at this , when user hasn't rated you just save it as you did before ! 看看这个,当用户没有评价你只是像以前一样保存它!

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

and in the update part ! 并在更新部分! do as follows : 做如下:

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

For registration, Eloquent was not running the update because the method I used (see below) did not set the exists attribute to true. 对于注册,Eloquent没有运行更新,因为我使用的方法(见下文)没有将exists属性设置为true。 This is because I was getting all the fields returned ($ request-> all) from the request and creating a model, but that does not enable the object to update. 这是因为我从请求中获取了所有返回的字段($ request-> all)并创建了一个模型,但这并不能使对象更新。

This did not work 这没用

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

This also did not work 这也行不通

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

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

Never use get_object_var , Eloquent Model has virtual attribute (you get with getAttributes), so get_object_var did not return your fields. 永远不要使用get_object_varEloquent Model有虚拟属性(你得到getAttributes),所以get_object_var没有返回你的字段。

This work!! 这项工作!!

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

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

I also faced same problem, so it turns out to be eloquent model issue. 我也遇到了同样的问题,所以事实证明这是一个雄辩的模型问题。

First of all add a protected array name fillable in your model and define the attributes.Like 首先在模型中添加一个可填充的受保护数组名称并定义属性

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

There are many way to update table in Laravel in your case. 在您的情况下,有很多方法可以更新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()); 

In place of update you can also use fill like. 代替更新,您也可以使用填充。

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

There is always DB::table method but use this only when no option is available. 始终存在DB :: table方法,但仅在没有可用选项时才使用此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM