Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Penaf's avatar
Level 1

isVideo or isImage funcion on Multimedia model based on a belongsTo

Having this model for Multimedia files:

class Multimedia extends Model
{

    protected $table = 'multimedias';

    public $timestamps = false;

    public function tipo_multimedia()
    {
        return $this->belongsTo('App\TipoMultimedia', 'tipo_multimedia_id');
    }

}

Where tipos_multimedias (in english: multimedia types) table has records like this

id  |  nome
---------------------------------------
1   | Images of landscapes
2   | Videos of animals
3   | Videos of landscapes
4   | Images of vertebrate animals

Is it possible to add to Multimedia model function like isVideo and isImage based on what's the ID on tipos_multimedia table?

Or would be simpler to add a tinyint(1) field called isImage on tipo_multimedias tables so I'd know if it was 0 it would be an image and 1 for videos? But for this the first question is still valid.

Thanks in advace!

0 likes
3 replies
Cronix's avatar
Cronix
Best Answer
Level 67

I'd add a media_categories table so it's more dynamic. Otherwise, if you add a new type with id of 5, you'd have to go in and add it into your code. Whereas if you had them in a different table, you could just do something like $media->category->type and get back "video" or "audio" or whatever else you may have.

1 like
Penaf's avatar
Level 1

The multimedias table is only for images and videos. I was thinking of adding a column isImage on tipos_multimedia table that would be 0 for image and 1 for video (I think it's faster than creating a new table and more relations)

I was asking because I think I saw on some tutorial people having functions similar to the ones I want (isImage and isVideo) for faster verification if a multimedia is an image or a video. But I don't know if that's the faster path

Cronix's avatar

Yes of course you can add a int column, and it will work fine. It's just not very intuitive. I don't like looking at data and having to remember "hmm, this 1...does that mean video, image, pdf, or what?" Think about yourself stepping away from the project for a year, and then coming back to it. Will you remember all of that stuff? "video" or "audio" would be crystal clear. An integer wouldn't (unless it's a foreign key pointing to the table with the real value).

As I say though, it will work totally fine.

1 like

Please or to participate in this conversation.