Created
September 8, 2016 12:36
-
-
Save Oldenborg/d106fd84d88f5a8522a9bd6c3ca37ff4 to your computer and use it in GitHub Desktop.
Laravel - Boolean toggle trait
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Traits; | |
trait ToggleTrait | |
{ | |
public function toggle($values, $bool = null) | |
{ | |
if(is_string($values)) | |
{ | |
$values = [$values]; | |
} | |
foreach($values as $value) | |
{ | |
$data[$value] = ($bool != null ? $bool : !$this->$value); | |
} | |
return $this->update($data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment