简体   繁体   English

Laravel-雄辩的创建或更新模型

[英]Laravel - Eloquent Create or Update Model

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- 但没有任何结果,我发现没有这样的回应-

在此处输入图片说明

Can anyone please help? 谁能帮忙吗?

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

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

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