简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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