SptpdRincianReklame.php 1.07 KB
<?php

/**
 * @Author: irul
 * @Date:   2019-09-23 15:29:45
 * @Last Modified by:   irul
 * @Last Modified time: 2019-10-01 23:43:21
 */

namespace Integrasi\Reklame\Base\Models;

use \Illuminate\Database\Capsule\Manager as DB;

class SptpdRincianReklame extends AbstractModel
{
	protected $table   = 'pad_sptpd_rk_det';
	protected $guarded = array();

	public $timestamps = false;

	protected static function boot()
	{
		parent::boot();
		static::creating(function ($model) {
			return $model->_onCreating();
		});
	}

	public function _onCreating()
	{
		$maxReklameKe = call_user_func($this->getParentNamespace() . '\SptpdRincianReklame::select',
			DB::raw('max(reklame_ke) as nomor'))
			->where(DB::raw('sptpd_id'), $this->attributes['sptpd_id'])
			->first();
		$reklameKe = ($maxReklameKe->nomor ?: 0) + 1;

		$this->attributes['reklame_ke'] = $reklameKe;
		return true;
	}

	// == RELATIONSHIPS == //

	/**
	 * Get the sptpd that owns the sptpd_rincian_reklame.
	 */
	public function sptpd()
	{
		return $this->belongsTo($this->getParentNamespace() . '\Sptpd', 'id', 'sptpd_id');
	}
}