Nicholas Vigo
Nicholas Vigo

Reputation: 11

Laravel cannot store data into database

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?

Upvotes: 1

Views: 62

Answers (2)

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

$time->event_id = '1';

should be

 $time->events_id = '1'; # 'event' <> 'events'

Use try{} catch{} in future for debugs

public function store3(){
    try {
        $time = new Time;
        $time->leaderboard_id = '1';
        $time->user_id = '1';
        $time->events_id = '1';
        $time->time = '1';
        $time->avg = '1';
        $time->save();

    } catch (\Exception $e) {
        dd($e->getMessage());
    }
    return $this->scramble3(0);
}

Upvotes: 0

Piyush Sapariya
Piyush Sapariya

Reputation: 538

Try this,

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);
}

Your schema is $table->unsignedBigInteger and $table->float but you passed data as string $time->leaderboard_id = '1';

Upvotes: 0

Related Questions