0

I have status const in the model and I am storing the status as integer in the table.

CONST STATUS_DONE = 0;
CONST STATUS_NEW = 1;
CONST STATUS_PROCESSING = 2;

When user make an API request,they pass in the string status instead of integer. Example Request Class:

public function rules()
{
    return [
        'name'      => 'required',
        'status'    => 'required|in:done,new,processing',
    ];
}

Problem is I am trying to figure out what the good approach to convert string status to int to be stored in table, eg:

Task::create([
  'name' => $request->name,
  'status' => // convert to int?
]);
7
  • Try this 'status' => 'required|integer|in:done,new,processing' Commented Feb 13, 2018 at 14:02
  • OR simple you can do (int)$request->name Commented Feb 13, 2018 at 14:04
  • That don't make sense. User will always pass in the status as string. The create() method need to be converted to int. Commented Feb 13, 2018 at 14:11
  • When you say they pass in the string status instead of integer do you mean '0', '1', '2' OR 'Done', 'New', 'Processing'? Commented Feb 13, 2018 at 14:19
  • @waterloomatt 'Done', 'New', 'Processing' Commented Feb 13, 2018 at 14:28

1 Answer 1

1

You can set up a mapping array which maps the incoming status to an integer. Make sure to do your validation first and also consider case issues, spacing etc. which will cause not matches.

<?php
$mapping = [
    'done' => STATUS_DONE,
    'new' => STATUS_NEW,
    'processing' => STATUS_PROCESSING 
];

$statusInteger= $mapping[$request->status];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.