Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
Defining the values of a field in a database in the Laravel code to make the code more readable

Currently, I am writing a laravel application with a part for sending messages between the staff at the company and the clients.
So, I have a field named "status" in the database. In this field, a value of one indicates that the message is waiting for an answer, a value of two indicates that it has been answered, and a value of three indicates that the message has been closed. There is a problem here, however. It's not clear what these numbers do when someone looks at my code.
Would there be any way for me to define this number or any other way to make my code more readable?
(I'm using laravel eloquent ORM) The code below is for the method that closes a conversation:

    public function close(Request $request)
    {
        $message = Message::find($request->message_id);
//        Status one indicates that a conversation has been closed
        $message->status = 1;
        $message->save();
        return \response($message, 200);
    }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Use constants in your Message model

class Message
{
    const STATUS_PENDING = 1;
    const STATUS_ANSWERED = 2;
    const STATUS_CLOSED = 3;
//...
}

Then your code will be readable

public function close(Request $request)
{
    $message = Message::find($request->message_id);
    $message->status = Message::STATUS_CLOSED;
    $message->save();
    return \response($message, 200);
}

Or Even better, make it a method in your model on top of the constants values

public function close(Request $request)
{
    $message = Message::find($request->message_id);
    $message->close();
    return \response($message, 200);
}

That way you can in the future upgrade the method, for example

class Message
{
    public function close()
    {
        if ($this->status != self::STATUS_ANSWERED) {
            //log message closed without client answer
        }
        $this->status = STATUS_CLOSED;
        $this->save();
    }
}
over 4 years ago · Santiago Trujillo Report

0

You could use something like this. First we crate some static variables in the model, representing your conversation status:

public static $_STATUS_PENDING = 1;
public static $_STATUS_ANSWERED = 2;
public static $_STATUS_CLOSED = 3;

When you add those, you will be able to call them statically now:

$message->status = Message::$_STATUS_PENDING; // 1
$message->status = Message::$_STATUS_ANSWERED; // 2
$message->status = Message::$_STATUS_CLOSED; // 3

This is a lot more readable now.

You can go even further with this, and create another array that you will use to display these values, without the need to do if-else statements. Fist we will create a key-value array that will represent the values:

public static function getStatuses()
{
    return [
        self::$_STATUS_PENDING    => 'Pending',
        self::$_STATUS_ANSWERED => 'Answered',
        self::$_STATUS_CLOSED    => 'Closed',
    ];
}

After that, we will create a function that you will be able to call on your Message instance, that will display the status of message:

public function getMessageStatus()
{
    return self::getStatuses()[$this->status];
}

Now, when we have this method, we can simply call it and get the correct status of the message:

$message->getMessageStatus(); //Pending, Answered or Closed
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!