Skip to content

Instantly share code, notes, and snippets.

@hmpmarketing
Created September 26, 2018 20:30
Show Gist options
  • Save hmpmarketing/598c1ba8fbe26ff2fab787cdabfb1736 to your computer and use it in GitHub Desktop.
Save hmpmarketing/598c1ba8fbe26ff2fab787cdabfb1736 to your computer and use it in GitHub Desktop.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CampaignPlay extends Model
{
public $primaryKey = false;
public $incrementing = false;
protected $guarded = [];
/**
* Get the campaign that owns the campaign_play.
*/
public function campaign()
{
return $this->belongsTo('App\Models\Campaign');
}
public function scopeByCampaignId($query, $campaignId)
{
return $query->where('campaign_id', '=', $campaignId);
}
public function scopeToday($query)
{
return $query->where('campaign_date', '=', date('Y-m-d'));
}
public static function incrementPlayCount($campaignId)
{
$count = self::byCampaignId($campaignId)->today()->increment('play_count');
if ($count == 0) {
self::create([
'campaign_id' => $campaignId,
'campaign_date' => date('Y-m-d'),
'play_count' => 1
]);
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment