. */ namespace App\Commands; use App\Models\Playlist; use App\Models\Track; use Auth; use DB; class RemoveTrackFromPlaylistCommand extends CommandBase { private $_track; private $_playlist; public function __construct($playlistId, $trackId) { $this->_playlist = Playlist::find($playlistId); $this->_track = Track::find($trackId); } /** * @return bool */ public function authorize() { $user = Auth::user(); return $user != null && $this->_playlist && $this->_track && $this->_playlist->user_id == $user->id; } /** * @throws \Exception * @return CommandResponse */ public function execute() { $this->_playlist->tracks()->detach($this->_track); Playlist::whereId($this->_playlist->id)->update([ 'track_count' => DB::raw('(SELECT COUNT(id) FROM playlist_track WHERE playlist_id = '.$this->_playlist->id.')'), ]); return CommandResponse::succeed(['message' => 'Track removed!']); } }