|
|
@ -423,14 +423,15 @@ abstract class Model { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Load Model data: all, with a limit or pagination |
|
|
|
* Load all Models, optionally with a limit or pagination |
|
|
|
* |
|
|
|
* |
|
|
|
* @param int $limitOrPage Treated as page if $limit is provided, limit otherwise |
|
|
|
* @param int $limitOrPage Treated as page if $limit is provided, limit otherwise |
|
|
|
* @param int $limit The amount to limit by |
|
|
|
* @param int $limit The amount to limit by |
|
|
|
* |
|
|
|
* |
|
|
|
* @return array|null The found model data, or null |
|
|
|
* @return array|null The found model data, or null |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function all(int $limitOrPage = -1, int $limit = -1): ?array { |
|
|
|
public static function all(int $limitOrPage = -1, int $limit = -1): ?array |
|
|
|
|
|
|
|
{ |
|
|
|
$class = get_called_class(); |
|
|
|
$class = get_called_class(); |
|
|
|
$model = new $class; |
|
|
|
$model = new $class; |
|
|
|
|
|
|
|
|
|
|
@ -494,7 +495,8 @@ abstract class Model { |
|
|
|
* |
|
|
|
* |
|
|
|
* @return Model The Model |
|
|
|
* @return Model The Model |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function create(array $data): Model { |
|
|
|
public static function create(array $data): Model |
|
|
|
|
|
|
|
{ |
|
|
|
$class = get_called_class(); |
|
|
|
$class = get_called_class(); |
|
|
|
$model = new $class; |
|
|
|
$model = new $class; |
|
|
|
$model->fill($data); |
|
|
|
$model->fill($data); |
|
|
@ -503,17 +505,20 @@ abstract class Model { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Retreive Model, or create |
|
|
|
* Retreive Model, create if it doesn't exist |
|
|
|
* |
|
|
|
* |
|
|
|
* Usage: |
|
|
|
* Usage: |
|
|
|
* $model = \App\Model\Example::firstOrCreate(['name' => 'Example name']); |
|
|
|
* $model = \App\Model\AddressModel::firstOrCreate( |
|
|
|
|
|
|
|
* ['zip_code' => '1234AB', 'house_number' => 3], |
|
|
|
|
|
|
|
* ['street' => 'Example lane']); |
|
|
|
* |
|
|
|
* |
|
|
|
* @param $search Retrieve by |
|
|
|
* @param $search Retrieve by |
|
|
|
* @param $data Instantiate with search plus data |
|
|
|
* @param $data Data used for creation |
|
|
|
* |
|
|
|
* |
|
|
|
* @return Model The Model |
|
|
|
* @return Model The Model |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function firstOrCreate(array $search, array $data = []): Model { |
|
|
|
public static function firstOrCreate(array $search, array $data = []): Model |
|
|
|
|
|
|
|
{ |
|
|
|
$model = self::firstOrNew($search, $data); |
|
|
|
$model = self::firstOrNew($search, $data); |
|
|
|
|
|
|
|
|
|
|
|
if (!$model->exists()) { |
|
|
|
if (!$model->exists()) { |
|
|
@ -523,14 +528,27 @@ abstract class Model { |
|
|
|
return $model; |
|
|
|
return $model; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// // Update existing Model, or create it if doesn't exist |
|
|
|
/** |
|
|
|
// public static function updateOrCreate(array $data, array $data): Model { |
|
|
|
* Update Model, create if it doesn't exist |
|
|
|
// // $flight = App\Flight::updateOrCreate( |
|
|
|
* |
|
|
|
// // ['departure' => 'Oakland', 'destination' => 'San Diego'], |
|
|
|
* Usage: |
|
|
|
// // ['price' => 99] |
|
|
|
* $model = \App\Model\FlightModel::updateOrCreate( |
|
|
|
// // ); |
|
|
|
* ['departure' => 'Oakland', 'desination' => 'San Diego'], |
|
|
|
// return new Model; |
|
|
|
* ['price' => 99]); |
|
|
|
// } |
|
|
|
* |
|
|
|
|
|
|
|
* @param $search Retrieve by |
|
|
|
|
|
|
|
* @param $data Data used for creation |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return Model The Model |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static function updateOrCreate(array $search, array $data): Model |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$model = self::firstOrNew($search, $data); |
|
|
|
|
|
|
|
$model->fill($data); |
|
|
|
|
|
|
|
$model->save(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $model; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|