. */ namespace App\Models; use Illuminate\Database\Eloquent\Model; /** * App\Models\ResourceUser * * @property integer $id * @property integer $user_id * @property integer $track_id * @property integer $album_id * @property integer $playlist_id * @property integer $artist_id * @property boolean $is_followed * @property boolean $is_favourited * @property boolean $is_pinned * @property integer $view_count * @property integer $play_count * @property integer $download_count * @method static \Illuminate\Database\Query\Builder|\App\Models\ResourceUser whereId($value) * @method static \Illuminate\Database\Query\Builder|\App\Models\ResourceUser whereUserId($value) * @method static \Illuminate\Database\Query\Builder|\App\Models\ResourceUser whereTrackId($value) * @method static \Illuminate\Database\Query\Builder|\App\Models\ResourceUser whereAlbumId($value) * @method static \Illuminate\Database\Query\Builder|\App\Models\ResourceUser wherePlaylistId($value) * @method static \Illuminate\Database\Query\Builder|\App\Models\ResourceUser whereArtistId($value) * @method static \Illuminate\Database\Query\Builder|\App\Models\ResourceUser whereIsFollowed($value) * @method static \Illuminate\Database\Query\Builder|\App\Models\ResourceUser whereIsFavourited($value) * @method static \Illuminate\Database\Query\Builder|\App\Models\ResourceUser whereIsPinned($value) * @method static \Illuminate\Database\Query\Builder|\App\Models\ResourceUser whereViewCount($value) * @method static \Illuminate\Database\Query\Builder|\App\Models\ResourceUser wherePlayCount($value) * @method static \Illuminate\Database\Query\Builder|\App\Models\ResourceUser whereDownloadCount($value) * @mixin \Eloquent */ class ResourceUser extends Model { protected $table = 'resource_users'; public $timestamps = false; public static function get($userId, $resourceType, $resourceId) { $resourceIdColumn = $resourceType.'_id'; $existing = self::where($resourceIdColumn, '=', $resourceId)->where('user_id', '=', $userId)->first(); if ($existing) { return $existing; } $item = new ResourceUser(); $item->{$resourceIdColumn} = $resourceId; $item->user_id = $userId; return $item; } public static function getId($userId, $resourceType, $resourceId) { $item = self::get($userId, $resourceType, $resourceId); if ($item->exists) { return $item->id; } $item->save(); return $item->id; } }