Use of constants in a model

Posted 5 days ago by oliverbusk

Hi all

I have a basic model called Document. A document can be two things at the moment:

  1. A PDF file
  2. An image file

Currently, I have a column in my documents table called filetype. This column holds the filetype from the uploaded document, like so:

'filetype' => request()->file->getClientOriginalExtension()

However, I've noticed that for a PDF file, for example, the extension can be pdf or PDF.

This is causing me some annoying "bugs" in my code. Imagine:

if ($this->document->filetype === 'pdf') {
 // Do something

Now above will works for all PDF files that have the filetype with lowercase pdf.

A workaround could be to just use strtolower($this->document->filetype) === 'pdf') - or not use strict checking.

However this got me thinking - can this be solved by using CONSTS?

For example in my Documents model:

CONST IMAGE_FILE = 'jpeg,jpg,gif';

I know above is not valid code - but I was thinking if something like this was possible within Laravel?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.