Reputation: 173
I am creating a table that has an enum type of various options that are registered in the model.
MIGRATION
Schema::create('timelines', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('timelineable_id');
$table->string('timelineable_type');
$table->enum('type', [Timeline::SURVEY, Timeline::COMMENT, Timeline::SHAREPOST, Timeline::FOLLOW, Timeline::SHARESOCIAL]);
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
MODEL TIMELINE
class Timeline extends Model
{
use HasFactory;
protected $guarded = ['id'];
const SURVEY = 1;
const COMMENT = 2;
const SHAREPOST = 3;
const FOLLOW = 4;
const SHARESOCIAL = 5;
public function timelineable()
{
return $this->morphTo();
}
}
The problem comes when I insert a record in the table, it creates everything fine except the "type" value which always adds the value Timeline::xxx -1 .
$commentCreate->timelines()->create([
'user_id' => Auth::user()->id,
'type' => Timeline::COMMENT,
]);
Does anyone know why this might be happening?
Upvotes: 0
Views: 47
Reputation: 1384
Try this The value of //$EnumValue = 2
$value = "COMMENT";
$Enumvalue = Timeline ::enum($value);
$commentCreate->timelines()->create([
'user_id' => Auth::user()->id,
'type' => $Enumvalue,
]);
class Timeline extends Model
{
use HasFactory;
protected $guarded = ['id'];
const SURVEY = 1;
const COMMENT = 2;
const SHAREPOST = 3;
const FOLLOW = 4;
const SHARESOCIAL = 5;
public function enum($string){
return constant('Timeline ::'.$string);
}
}
Upvotes: 1