I am trying to create a custom attribute inside of a Laravel Model. This attribute would be the same for all of the Model instances (static?). The goal is to use this attribute to populate a select dropdown when creating a Model instance. Example:
class User extends Model
{
protected $table = 'users';
protected $guarded = [ 'id' ];
public $timestamps = true;
protected $genders = ['male'=>'Male', 'female'=>'Female']; //custom
public static function getGenderOptions() {
return $genders;
}
}
Then when building out the form, I could do something like:
// UserController.php
$data['select_options'] = User::getGenderOptions();
return view('user.create', $data);
// create.blade.php
{!! Form::select( 'gender', $select_options ) !!}
This causes me to get the error:
Undefined variable: genders
I am trying to prevent cluttering my Controller with all of the select options, as there are also a few others I haven't included.
Thanks.