akaunting/app/Models/Common/Dashboard.php

72 lines
1.6 KiB
PHP
Raw Normal View History

2019-11-16 07:21:14 +00:00
<?php
namespace App\Models\Common;
use App\Abstracts\Model;
use Bkwld\Cloner\Cloneable;
2020-10-16 20:35:26 +00:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2019-11-16 07:21:14 +00:00
class Dashboard extends Model
{
2020-10-16 20:35:26 +00:00
use Cloneable, HasFactory;
2019-11-16 07:21:14 +00:00
protected $table = 'dashboards';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
2020-01-07 14:15:00 +00:00
protected $fillable = ['company_id', 'name', 'enabled'];
2019-11-16 07:21:14 +00:00
2020-11-13 12:15:27 +00:00
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'enabled' => 'boolean',
];
2019-11-16 07:21:14 +00:00
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['name', 'enabled'];
2020-01-07 14:15:00 +00:00
public function users()
2019-11-16 07:21:14 +00:00
{
2020-01-07 14:15:00 +00:00
return $this->morphedByMany('App\Models\Auth\User', 'user', 'user_dashboards', 'dashboard_id', 'user_id');
2019-11-16 07:21:14 +00:00
}
2019-12-31 08:27:49 +00:00
public function widgets()
2019-11-16 07:21:14 +00:00
{
2019-12-31 08:27:49 +00:00
return $this->hasMany('App\Models\Common\Widget')->orderBy('sort', 'asc');
2019-11-16 07:21:14 +00:00
}
2020-10-16 20:35:26 +00:00
2021-01-29 20:56:25 +00:00
/**
* Scope to only include dashboards of a given user id.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $user_id
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeUserId($query, $user_id)
{
return $query->whereHas('users', function ($query) use ($user_id) {
$query->where('user_id', $user_id);
});
}
2020-10-16 20:35:26 +00:00
/**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
protected static function newFactory()
{
return \Database\Factories\Dashboard::new();
}
2019-11-16 07:21:14 +00:00
}