我正在尝试在Laravel中使用ajax请求,该请求基于select2输入来搜索记录。现在,一旦获取数据,我将对其进行转换,仅用于加密ID。这是我获取记录并转换其ID进行加密的方式。
public function autocompletePedigree($subdomain, $type = null)
{
$q = request()->get('q');
$sex = NULL;
if (!is_null($type)) {
$sex = $this->sexesRepository->make()->where('name', $type)->first(['id'])->id;
}
$results = $this->dogsRepository->search($q, $sex);
$results->getCollection()->transform(function ($value) {
$value->id = encrypt($value->id);
return $value;
});
return $results;
}
现在结果包含一个狗的名字和ID,当我使用dd($results)
时,ID被转换并正确了,这是一个示例
#attributes: array:2 [▼
"id" => "eyJpdiI6IlN5YXdzT3hReEc2bnczRGM5YzV1eGc9PSIsInZhbHVlIjoiZ0NpaTRGWjNmS1ZEQjVZcGFZeWJRdz09IiwibWFjIjoiMThmNmQ5YmE5ZDI1MDg4OTZhMmExMTlmNzM0ZGU3ZDRhNzg0OGFhNzM2OTViYzVkN2QxZTUzMzMwMWY4ZDdhYiJ9"
"text" => "Koffiewater Chicco"
]
但是当我使用return response()->json($results)
切换到JSON时,它会返回此
"data":[
{
"id":0,
"text":"Koffiewater Chicco"
},
{
"id":0,
"text":"Koffiewater Columbus"
}
我不明白为什么将id变成0。这是某种JSON清理过程吗?有人可以帮我将加密的ID保留在JSON响应中吗?
更新:已添加模型
<?php
namespace BBAfrica\Clubs\Repositories\Dogs;
use DB;
use BBAfrica\Clubs\Extensions\Model;
use App\AuditTrail;
class Dogs extends Model {
protected $table = 'dogs';
/**
* Properties that can be mass assigned
*
* @var array
*/
protected $fillable = [
'breeder_id',
'dog_no',
'dog_name',
'dog_score',
'dog_image',
'sex_id',
'ba_status_id',
'added_by',
'breed_id'
];
public function sex() {
return $this->hasOne('BBAfrica\Clubs\Repositories\Sexes\Sexes', 'id', 'sex_id');
}
public function ba_status() {
return $this->hasOne('BBAfrica\Clubs\Repositories\BaStatuses\BaStatuses', 'id', 'ba_status_id');
}
public function added() {
return $this->belongsTo('BBAfrica\Clubs\Repositories\User\User', 'added_by');
}
public function parents () {
return $this->hasOne('BBAfrica\Clubs\Repositories\Parents\Parents', 'dog_id', 'dog_no');
}
public function breeder () {
return $this->hasOne('BBAfrica\Clubs\Repositories\Breeders\Breeders', 'id', 'breeder_id');
}
public function clubDog () {
return $this->hasOne('BBAfrica\Clubs\Repositories\ClubDogs\ClubDogs', 'id', 'dog_id');
}
public function audit(){
return $this->morphOne(AuditTrail::class,'audit_trails');
}
}
扩展模型
<?php namespace BBAfrica\Clubs\Extensions;
use BBAfrica\Extensions\Model as BBAfricaModel;
use Carbon\Carbon;
class Model extends BBAfricaModel {
protected $hidden = array(
'updated_at',
'is_active',
'ss_sync_id'
);
/**
* List of all dates fields in database to create Carbon instances
*
* @return array
*/
public function getDates() {
return [
'created_at',
'updated_at',
'session_start_date',
'session_end_date',
'term_start_date',
'date_of_birth',
'employment_date',
'inoculation_date',
'start_date',
'end_date',
'event_date',
];
}
}
BBAfricaModel
<?php namespace BBAfrica\Extensions;
use \BBAfrica\Contexts\Context;
use \Illuminate\Database\Eloquent\Model as EloquentModel;
class Model extends EloquentModel {
/**
* Scope a query based upon a column name
*
* @param \BBAfrica\Contexts\Context $scope
* @return \Illuminate\Database\Eloquent\Model
*/
public function scopeColumn(Context $scope)
{
if($scope->has())
{
return $this->where($scope->column(), '=', $scope->id());
}
return $this;
}
/**
* Scope the query based upon a relationship
*
* @param \BBAfrica\Contexts\Context $scope
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeRelationship(Context $scope)
{
if($scope->has())
{
return $this->whereHas($scope->table(), function($q) use ($scope)
{
$q->where($scope->column(), '=', $scope->id());
});
}
return $this;
}
public function update(array $attributes = array(), array $options = array())
{
$attributes['sync_status'] = 0;
if ( ! $this->exists)
{
return $this->newQuery()->update($attributes);
}
return $this->fill($attributes)->save();
}
public function fill(array $attributes)
{
$attributes['sync_status'] = 0;
$totallyGuarded = $this->totallyGuarded();
foreach ($this->fillableFromArray($attributes) as $key => $value)
{
$key = $this->removeTableFromKey($key);
// The developers may choose to place some attributes in the "fillable"
// array, which means only those attributes may be set through mass
// assignment to the model, and all others will just be ignored.
if ($this->isFillable($key))
{
$this->setAttribute($key, $value);
}
elseif ($totallyGuarded)
{
throw new MassAssignmentException($key);
}
}
return $this;
}
}
经过一番挖掘后,我发现它已自动转换为整数,从而引起了一些混乱。因此,我通过将public $incrementing = false;
添加到Dogs
模型
对于其他好奇于此的人(如我):
他正在更改模型主键的类型。当他(或在这种情况下为JsonResponse
)在模型上调用toJson()
时,它将尝试将主键(已手动更改为string
)转换回其原始值(int
)。发生这种情况是因为主键实际上是模型的added to the $casts
property:
$casts
...并且当模型被序列化到数组(随后是JSON)时,属性被强制转换。