wanting to insert this data to database with laravel controller, but there’s no error message and it seems the data won’t be stored.
this is my controller function
public function store3(){
$time = new Time;
$time->leaderboard_id = '1';
$time->user_id = '1';
$time->event_id = '1';
$time->time = '1';
$time->avg = '1';
$time->save();
return $this->scramble3(0);
}
this is my migration table
Schema::create('time', function (Blueprint $table) {
$table->id();
$table->float("time");
$table->unsignedBigInteger('leaderboard_id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('events_id');
$table->float('avg');
$table->timestamps();
});
Schema::table('time', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate("cascade");
$table->foreign('leaderboard_id')->references('id')->on('leaderboards')->onDelete('cascade')->onUpdate("cascade");
$table->foreign('events_id')->references('id')->on('events')->onDelete('cascade')->onUpdate("cascade");
});
}
my model code
class Time extends Model
{
use HasFactory;
protected $table = "time";
protected $fillable = [
'id',
'user_id',
'leaderboard_id',
'events_id',
'time',
'avg'
];
public function User(){
return $this->hasOne(User::class, 'User_id','id');
}
public function Leaderboard(){
return $this->hasOne(Leadeboard::class, 'Leaderboard_id','id');
}
public function Event(){
return $this->hasOne(Event::class, 'Events_id','id');
}
}
can anyone help what’s wrong with it?
2
Answers
Try this,
Your schema is
$table->unsignedBigInteger
and$table->float
but you passed data as string$time->leaderboard_id = '1';
should be
Use
try{} catch{}
in future for debugs