2016-07-02 4 views
0

제품보기에 대한보기 카운터를 만들려고하므로 제품이 몇 번 열렸는지 추적해야합니다.Laravel - Eloquent 모델 생성 또는 업데이트

그래서, 내가 무슨 짓을하는 것은 컨트롤러 -

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

에서이 같은 새로운 테이블 -

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

그리고 호출을 만드는 것입니다하지만, 그것은 효과가 없었습니다.

은 다시 실패 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); 

뿐인데 시도하지만 아무것도 밖으로 작동하지, 나는 사람이 도움을 주시기 바랍니다 수 this-

enter image description here

처럼 더 responce을 발견하지입니까?

+0

는 Laravel은 저장소 디렉토리에 쓸 수 도움이 될 것 같아? 내가 할 수없는 경우 오류 (예 : '이 사이트에 연결할 수 없음') 화면이 표시되지 않는 경우가 있습니다. –

답변

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

행운, 그것은 동일합니다 : ( –