Laravel - 雄辩的创建或更新模型 [英] Laravel - Eloquent Create or Update Model

查看:196
本文介绍了Laravel - 雄辩的创建或更新模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

控制器 -

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

但它没有奏效。

然后尝试使用这个 -

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

再次失败。

然后尝试使用 -

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

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

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

任何人都可以帮忙?

推荐答案

我认为这将有助于您 -

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

这篇关于Laravel - 雄辩的创建或更新模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆