Laravel - Eloquent创建或更新模型

时间:2016-07-02 12:32:36

标签: php laravel eloquent query-builder

我正在尝试为产品视图创建一个视图计数器,因此我需要跟踪产品的打开次数。

所以,我所做的是创建一个新表 -

Schema::create('user_add_views', function (Blueprint $table)
{
    $table->integer('user_id')              ->unsigned()        ->index();
    $table->integer('add_id')       ->unsigned()        ->index();
    $table->integer('total_view')           ->integer()     ->default(1);
    //Removed all keys for making things faster
    //Foreign Keys
    $table->foreign('user_id')              ->references('id')  ->on('users');
    $table->foreign('add_id')       ->references('id')  ->on('advertisements');

    //Composite Primary Key
    $table->unique([
                        'user_id',
                        'add_id'
                    ],'user_add_views_ck');
});

一个新模型 -

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserAdvertisementView extends Model
{
    protected $primaryKey   =   null;
    public $incrementing    =   false;
    public $timestamps      =   false;

    protected $table        =   'user_add_views';           //Table Name

    protected $fillable     =   [
                                    'user_id',
                                    'add_id',
                                    'total_view'
                                ];

    public function user()
    {
        return $this->hasOne('App\User','user_id');
    }

    public function advertisement()
    {
        return $this->hasOne('App\advertisement','add_id');
    }

    //Scope Query for view count
    /**
    * Scope a query for a mass Assignment
    *
    * @return \Illuminate\Database\Eloquent\Builder
    */
   /*
   public function scopeInsertData($query,$project_id,$data)
   {
        $bulk_data = array();
        foreach ($data as $value)
        {
            array_push(
                            $bulk_data,
                            array(
                                    'project_id' => $project_id,
                                    'feature_id' => $value
                                )
                        );
        }
       return $query->insert($bulk_data);
    }
    */
}

从控制器这样调用 -

UserAdvertisementView::firstOrCreate([
                                    'user_id'   =>  Auth::user()->id,
                                    'add_id'    =>  $requestData['product_id']
                                ])->increment('total_view');

但它没有用。

然后尝试了这个 -

$counter = UserAdvertisementView::firstOrNew([
                                'user_id'   =>  Auth::user()->id,
                                'add_id'    =>  $id
                            ]);

if ($counter->exists)
{ // some way to check
    $counter->total_view = $counter->total_view+1;
    $counter->save();
    return $counter;
}
else
{
    $counter->save();
    return "Created";
}

再次失败。

然后尝试用 -

$counter->increment('total_view',1);

但没有任何结果,我发现没有这样的反应 -

enter image description here

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我认为它会对你有帮助 -

$user = UserAdvertisementView::where('user_id','=',Auth::user()->id)
              ->where('add_id',$requestData['product_id'])
              ->first();

if(isset($user)){
    $user->increment('total_view');
}else{
    UserAdvertisementView::create(array(
    'user_id'=> Auth::user()->id,
    'add_id'=> $requestData['product_id'],
    'total_view'=>1
    ));
}