Tengo una tabla que tiene varios campos date e int.
El controlador al momento de hacer store:
public function store(DrawRequest $request)
{
return response(new DrawResource(Draw::create($request->all())))
->setStatusCode(201);
}
El modelo:
class Draw extends Model
{
use SoftDeletes;
protected $table = 'draws';
protected $fillable = [
'start_date', 'end_date', 'complete_to_time', 'complete_to_date', 'tournament_id', 'calendar_id',
'state', 'prize_money', 'registration_price'
];
protected $dates = ['start_date', 'end_date','complete_to_date'];
protected $dispatchesEvents = [
'created' => DrawCreatedEvent::class,
'updated' => DrawUpdatedEvent::class,
'deleted' => DrawDeletedEvent::class,
El resource:
class Draw extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'success' => true,
'data' => [
'success' => true,
'data' => $this->resource
]
];
}
}
y al momento de retornar los datos me devuelve todos como string menos el id:
{
"success": true,
"data": {
"success": true,
"data": {
"start_date": "2019-10-19 00:00:00",
"end_date": "2019-10-20 00:00:00",
"tournament_id": "1",
"calendar_id": "1",
"updated_at": "2019-06-10 10:06:55",
"created_at": "2019-06-10 10:06:55",
"id": 5
}
}
}
la unica forma de cambiar esto es castear al tipo de dato correcto en el resource, pero es tedioso, haciendo algo como esto:
protected $casts = [
'tournament_id' => 'integer',
'calendar_id' => 'integer',
'prize_money' => 'prize_money',
'state' => 'boolean'
];