duanhuiuw12555 2016-07-02 12:32
浏览 87

Laravel - Eloquent创建或更新模型

I am trying to create a view counter for product views, so I need to track how many times the product is opened.

So, what I have done is create a new table-

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

And a new model-

<?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);
    }
    */
}

And calling like this from controller-

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

But it did not worked.

Then tried with this-

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

Again failed.

Then tried with-

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

But nothing works out, and I am finding no responce like this-

enter image description here

Can anyone please help?

  • 写回答

1条回答 默认 最新

  • douchui4815 2016-07-02 12:46
    关注

    I think it will help you-

    $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
        ));
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP