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

Kris01's avatar

How To Acess protected in model?

How can I access a protected variable in model from controller ? My model looks like this

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    use HasFactory;
    protected $guarded = [];
    protected $statuses = ['Complte', 'Pending', 'Failed', 'Processing'];
    protected $shift_types = ['Day','Night', 'Holiday'];
    
}

0 likes
11 replies
tykus's avatar

Make a getter:

public function getStatuses()
{
   return $this->statuses;
}
public function getShiftTypes()
{
   return $this->shift_types;
}
1 like
vincent15000's avatar

I think that it's like a private property, but visible in all classes that inherit from this one.

If you need to access one of those properties, you have to create an accessor in the model.

// Model
public function getShiftTypes()
{
	return $this->shift_types;
}
...

// In the controller
$employee = new Employee;
$types = $employee->getShiftTypes();
tykus's avatar

@vincent15000 that's not an accessor; it is a getter. An Accessor has a very particular method naming convention; and also receives an existing attribute value if it the name matches.

@kris01 what did you not like about the earlier answers; you know the correct ones?

2 likes
tykus's avatar

@Kris01 why do you need to access this protected data in a Controller action? Are you missing an Enum class from your application perhaps?

1 like
Kris01's avatar

@tykus Instead of creating a 'statuses' table I just added them as variables in the model

1 like

Please or to participate in this conversation.