Last active
August 10, 2019 09:37
-
-
Save ralbear/336e5db2df7508d85ef29ae906cd1d86 to your computer and use it in GitHub Desktop.
Laravel assign and cast API request keys to table columns
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; | |
use Illuminate\Support\Str; | |
use Illuminate\Support\Facades\Hash; | |
trait ColumnsFromRequestTrait | |
{ | |
public function fillReceivedData() | |
{ | |
$parametersBody = get_object_vars(json_decode(request()->getContent())); | |
$invalidKeys = array_diff(array_keys($parametersBody), array_keys($this->keysToColumns)); | |
if (!empty($invalidKeys)) { | |
//TODO: Return error | |
dd('This keys are not allowed: ' . join(', ', $invalidKeys)); | |
} | |
foreach ($this->keysToColumns as $key => $parameters) { | |
if (!empty($paramenters) || $parameters != []) { | |
if (array_key_exists($key, $parametersBody)) { | |
if ($parameters['type'] == 'string') { | |
$this->{$parameters['columnName']} = (string) $parametersBody[$key]; | |
} else if ($parameters['type'] == 'integer') { | |
$this->{$parameters['columnName']} = (int) $parametersBody[$key]; | |
} else if ($parameters['type'] == 'boolean') { | |
$this->{$parameters['columnName']} = (bool) $parametersBody[$key]; | |
} else if ($parameters['type'] == 'json') { | |
$originalContent = json_decode($this->{$parameters['columnName']}); | |
$newParameters = json_decode(json_encode($parametersBody[$key]), true);; | |
$this->{$parameters['columnName']} = array_merge((array) $originalContent, (array) $newParameters); | |
} else if ($parameters['type'] == 'password') { | |
$this->{$parameters['columnName']} = Hash::make($parametersBody[$key]); | |
} else { | |
$this->{$parameters['columnName']} = $parametersBody[$key]; | |
} | |
} else if (request()->method() == 'PUT') { | |
if (array_key_exists('default', $parameters)) { | |
$this->{$parameters['columnName']} = $parameters['default']; | |
} else { | |
if ($parameters['type'] == 'string') { | |
$this->{$parameters['columnName']} = ''; | |
} else if ($parameters['type'] == 'integer') { | |
$this->{$parameters['columnName']} = 0; | |
} else if ($parameters['type'] == 'boolean') { | |
$this->{$parameters['columnName']} = false; | |
} else if ($parameters['type'] == 'json') { | |
$this->{$parameters['columnName']} = []; | |
} else if ($parameters['type'] == 'password') { | |
$this->{$parameters['columnName']} = Hash::make(Str::random(20)); | |
} else { | |
$this->{$parameters['columnName']} = NULL; | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
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\Http\Controllers; | |
use App\Models\Item; | |
use Illuminate\Http\Request; | |
class ItemController extends Controller | |
{ | |
public function create(Request $request) | |
{ | |
$newItem = new Item(); | |
$newItem->fillReceivedData(); | |
$newItem->save(); | |
return $newItem; | |
} | |
} |
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\Models; | |
use App\Traits\ColumnsFromRequestTrait; | |
use Illuminate\Database\Eloquent\Model; | |
class ItemModel extends Model | |
{ | |
use ColumnsFromRequestTrait; | |
protected $casts = [ | |
'metadata' => 'array' | |
]; | |
protected $keysToColumns = [ | |
'typeId' => [ | |
'columnName' => 'type_id', | |
'type' => 'integer' | |
], | |
'alias' => [ | |
'columnName' => 'alias', | |
'type' => 'string' | |
], | |
'itemNumber' => [ | |
'columnName' => 'item_number', | |
'type' => 'integer' | |
], | |
'available' => [ | |
'columnName' => 'available', | |
'type' => 'boolean' | |
], | |
'price' => [ | |
'columnName' => 'price', | |
'type' => 'float' | |
], | |
'attributes' => [] | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment